java課程課後作業04之動手動腦
阿新 • • 發佈:2018-11-08
一.多層的異常捕獲-1
先貼出程式碼:
1 public class CatchWho { 2 public static void main(String[] args) { 3 try { 4 try { 5 throw new ArrayIndexOutOfBoundsException(); 6 } 7 catch(ArrayIndexOutOfBoundsException e) {8 System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); 9 } 10 11 throw new ArithmeticException(); 12 } 13 catch(ArithmeticException e) { 14 System.out.println("發生ArithmeticException"); 15 }16 catch(ArrayIndexOutOfBoundsException e) { 17 System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 18 } 19 } 20 }
執行結果:
ArrayIndexOutOfBoundsException/內層try-catch
發生ArithmeticException
然後我們在貼出一個修改之後的程式碼:
1 public classCatchWho2 { 2 public static void main(String[] args) { 3 try { 4 try { 5 throw new ArrayIndexOutOfBoundsException(); 6 } 7 catch(ArithmeticException e) { 8 System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); 9 } 10 throw new ArithmeticException(); 11 } 12 catch(ArithmeticException e) { 13 System.out.println("發生ArithmeticException"); 14 } 15 catch(ArrayIndexOutOfBoundsException e) { 16 System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); 17 } 18 } 19 }
再看編譯出來的結果:
ArrayIndexOutOfBoundsException/外層try-catch
由兩個執行程式的程式碼不同以及結果不同,我們可以簡單的推斷出一個小的猜測:當我們進行多層巢狀的異常丟擲捕獲時,一個異常的丟擲,應當緊接著這個異常的捕獲,否則丟擲和捕獲之間程式碼將沒有辦法執行。(僅個人猜測,如有錯誤,感謝指正)
二.多層的異常捕獲-2
貼出程式碼:
1 package Javaexperiment04test01; 2 public class EmbededFinally { 3 public static void main(String args[]) { 4 5 int result; 6 7 try { 8 9 System.out.println("in Level 1"); 10 11 12 try { 13 14 System.out.println("in Level 2"); 15 // result=100/0; //Level 2 16 17 try { 18 19 System.out.println("in Level 3"); 20 21 result=100/0; //Level 3 22 23 } 24 25 catch (Exception e) { 26 27 System.out.println("Level 3:" + e.getClass().toString()); 28 29 } 30 31 32 finally { 33 34 System.out.println("In Level 3 finally"); 35 36 } 37 38 39 // result=100/0; //Level 2 40 41 42 } 43 44 catch (Exception e) { 45 46 System.out.println("Level 2:" + e.getClass().toString()); 47 48 } 49 finally { 50 51 System.out.println("In Level 2 finally"); 52 53 } 54 55 // result = 100 / 0; //level 1 56 57 } 58 59 catch (Exception e) { 60 61 System.out.println("Level 1:" + e.getClass().toString()); 62 63 } 64 65 finally { 66 67 System.out.println("In Level 1 finally"); 68 69 } 70 71 } 72 73 }
執行結果為:
in Level 1
in Level 2
in Level 3
Level 3:class java.lang.ArithmeticException
In Level 3 finally
In Level 2 finally
In Level 1 finally
而當我們在將程式碼中不同位置的// result=100/0;取消註釋的時候我們會發現,程式碼執行的結果並不一樣,在之前的地方取消註釋我們會向上一個程式一樣,捕獲異常之前的程式碼都不會被執行,即使是finally語句。
總結:當有多層巢狀的finally時,異常在不同的層次丟擲 ,在不同的位置丟擲,可能會導致不同的finally語句塊執行順序。
三.finally語句塊一定會執行嗎?
先貼出程式碼:
1 package Javaexperiment04test01; 2 public class SystemExitAndFinally { 3 4 5 public static void main(String[] args) 6 { 7 8 try{ 9 10 11 System.out.println("in main"); 12 13 throw new Exception("Exception is thrown in main"); 14 15 //System.exit(0); 16 17 18 } 19 20 catch(Exception e) 21 22 { 23 24 System.out.println(e.getMessage()); 25 26 System.exit(0); 27 28 } 29 30 finally 31 32 { 33 34 System.out.println("in finally"); 35 36 } 37 38 } 39 40 41 }
執行結果:
in main
Exception is thrown in main
原因:在finally語句在被執行之前,程式已經被強制關閉,無法再次進行後續的編譯。