当前位置: 技术问答>java相关
能讲讲这道关于异常处理的题吗?
来源: 互联网 发布时间:2017-04-03
本文导语: 答案是B,为什么不选D?为什么不选E? 1. public class ExceptionTest { 2. class TestException extends Exception {} 3. public void runTest () throws TestException {} 4. public void test () /* Point X*/ { 5. runTest (); 6. } 7. } At poin...
答案是B,为什么不选D?为什么不选E?
1. public class ExceptionTest {
2. class TestException extends Exception {}
3. public void runTest () throws TestException {}
4. public void test () /* Point X*/ {
5. runTest ();
6. }
7. }
At point X on line 4, which code can be added to make the code compile?
A.throws Exception
B.catch (Exception e)
C.throws RuntimeException
D.catch (TestException e)
E.no code is necessary
1. public class ExceptionTest {
2. class TestException extends Exception {}
3. public void runTest () throws TestException {}
4. public void test () /* Point X*/ {
5. runTest ();
6. }
7. }
At point X on line 4, which code can be added to make the code compile?
A.throws Exception
B.catch (Exception e)
C.throws RuntimeException
D.catch (TestException e)
E.no code is necessary
|
两种选择:
public void test () throws Exception
//throws TestExceptio也行,留给调用test()的函数去try ,catch
{
runTest ();
}
或者
public void test ()
{
try
{
runTest ();
}
catch(Exception e)
{
...
}
}
public void test () throws Exception
//throws TestExceptio也行,留给调用test()的函数去try ,catch
{
runTest ();
}
或者
public void test ()
{
try
{
runTest ();
}
catch(Exception e)
{
...
}
}
|
朋友,你不会认为b是正确的吧!这也不符合语法呀!
还是那句话,bible 的答案不重要,关键是理解,明白!
还是那句话,bible 的答案不重要,关键是理解,明白!
|
只有A是正确的.,B是错误的答案
当然如果有throws TestException,那么这个答案也是正确的.
可以用下面的代码测试,如果能编译通过就是正确的.你自己可以试试
public class ExceptionTest {
class TestException extends Exception {}
public void runTest () throws TestException {}
public void test () throws Exception{//这里可以把替换的代码放进去.
runTest ();
}
}
当然如果有throws TestException,那么这个答案也是正确的.
可以用下面的代码测试,如果能编译通过就是正确的.你自己可以试试
public class ExceptionTest {
class TestException extends Exception {}
public void runTest () throws TestException {}
public void test () throws Exception{//这里可以把替换的代码放进去.
runTest ();
}
}