SCJP題庫第124題
Given:
11. static void test() throws RuntimeException{
12. try{
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch(Exception ex){ System.out.print("exception ");}
17. }
18. public static void main(String[] args){
19. try{test();}
20. catch(RuntimeException ex){System.out.print("runtime ");}
21. System.out.print("end ");
22. }
What is the result?
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
解說:
19行呼叫test方法,先印出test 。
14行直接拋出一個RuntimeException例外,並且由16行的catch補捉,印出exception 。
回到main,因為test方法有處理例外而正常結束,20行的catch不會作用。
執行21行,印出end
11. static void test() throws RuntimeException{
12. try{
13. System.out.print("test ");
14. throw new RuntimeException();
15. }
16. catch(Exception ex){ System.out.print("exception ");}
17. }
18. public static void main(String[] args){
19. try{test();}
20. catch(RuntimeException ex){System.out.print("runtime ");}
21. System.out.print("end ");
22. }
What is the result?
A. test end
B. Compilation fails.
C. test runtime end
D. test exception end
E. A Throwable is thrown by main at runtime.
Ans: D
解說:
19行呼叫test方法,先印出test 。
14行直接拋出一個RuntimeException例外,並且由16行的catch補捉,印出exception 。
回到main,因為test方法有處理例外而正常結束,20行的catch不會作用。
執行21行,印出end
Comments