SCJP題庫第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
 
解說:
14x.toString()錯誤,因為xnull,不能用null去操作一個不存在的物件方法toString
執行finally內的敘述之後,丟出nullPointerException例外回到main
20行的catch補捉此例外

Comments

Popular posts from this blog

Android+Google Map API v3 Geocoding(地址轉經緯度度