2009年2月17日 星期二

第128題

Given:
1. public class A{
2. public void Method1(){
3. try{
4. B b = new B();
5. b.Method2();
6. //more code here
7. }catch(TestException te){
8. throw new RuntimeException(te);
9. }
10. }
11. }
1. public class B{
2. public void method2() throws TestException{
3. //more code here
4. }
5. }
1. public class TestException extends Exception{
2. }
And given:
31. public void method(){
32. A a = new A();
33. a.method1();
34. }

Which statement is true if a TestException is thrown on line 3 of class B?
A. Line 33 must be called within a try block.
B. The exception thrown by method1 in class A is not required to be caught.
C. The method declared on line 31 must be declared to throw a RuntimeException.
D. On line 5 of class A, the call to method2 of class B does not need to be placed in a try/catch block.
答案:B
參考:9-2 Java例外事件與處理機制、9-3 使用throw自行產生例外事件、9-4 自訂例外類別、9-5 使用throws拋出例外事件、9-6 RuntimeException與CheckedException。

----------------------------------------------------------

method2()的exception 拋出給原呼叫式 第5行的b.method2(); 並由 Method1()的try / catch 接住.
然後丟出 RuntimeException(te).
原本拋出的TestException 已經被處理掉, 另RuntimeException不需要被處理.

沒有留言:

張貼留言