SCJP 1.6版考題 130
出自 陳富國維基館
Given: 11. static void test(){ 12. try{ 13. String x = null; 14. System.out.print(x.toString() + " "); 15. } 16. finally{System.out.print("finally ");} 17. } 18. public static void main(String[] args){ 19. try{test();} 20. catch(Exception ex){System.out.print("exception ");} 21. } What is the result? A. null B. finally C. null finally D. Compilation fails. E. finally exception
解答
Ans: E
解說:
14行x.toString()錯誤,因為x是null,不能用null去操作一個不存在的物件方法toString 執行finally內的敘述之後,丟出nullPointerException例外回到main… 由20行的catch補捉此例外…
|