SCJP 1.6版考題 140
出自 陳富國維基館
Given: 1. public class A{ 2. public void method1(){ 3. B b = new B(); 4. b.method2(); 5. //more code here 6. } 7. } 1. public class B{ 2. public void method2(){ 3. C c = new C(); 4. c.method3(); 5. //more code here 6. } 7. } 1. public class C{ 2. public void method3(){ 3. //more code here 4. } 5. } And given: 25. try{ 26. A a = new A(); 27. a.method1(); 28. }catch(Exception e){ 29. System.out.print("an error occurred"); 30. } Which two statements are true if a NullPointerException is thrown on line 3 of class C? (Choose two.) A. The application will crash. B. The code on line 29 will be executed. C. The code on line 5 of class A will execute. D. The code on line 5 of class B will execute. E. The exception will be propagated back to line 27.
解答
Ans: B E
解說:
當類別C中的第3行(method3)拋出NullPointerException 時 method3不正常結束後回到類別B的第4行(method2), 而method2並沒有處理這個例外而不正常結束,又回到類別A的method1方法,回到method1的呼叫者27行(E), 這個例外被28行的catch補捉到,執行第29行(B)
|