04異常處理 動手動腦
阿新 • • 發佈:2018-11-11
請閱讀並執行AboutException.java示例,然後通過後面的幾頁PPT瞭解Java中實現異常處理的基礎知識。
import javax.swing.*; class AboutException { public static void main(String[] a) { int i=1, j=0, k; k=i/j; try { k = i/j; // Causes division-by-zero exception //throw new Exception("Hello.Exception!");AboutExcaption.java} catch ( ArithmeticException e) { System.out.println("被0除. "+ e.getMessage()); } catch (Exception e) { if (e instanceof ArithmeticException) System.out.println("被0除"); else { System.out.println(e.getMessage()); } }finally { JOptionPane.showConfirmDialog(null,"OK"); } } }
執行截圖:
閱讀以下程式碼(CatchWho.java),寫出程式執行結果:
public class CatchWho { public static void main(String[] args) { try { try {CatchWho.javathrow new ArrayIndexOutOfBoundsException(); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }
ArrayIndexOutOfBoundsException/內層try-catch
發生ArithmeticException
寫出CatchWho2.java程式執行的結果
public class CatchWho2 { public static void main(String[] args) { try { try { throw new ArrayIndexOutOfBoundsException(); } catch(ArithmeticException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/內層try-catch"); } throw new ArithmeticException(); } catch(ArithmeticException e) { System.out.println("發生ArithmeticException"); } catch(ArrayIndexOutOfBoundsException e) { System.out.println( "ArrayIndexOutOfBoundsException" + "/外層try-catch"); } } }CatchWho2
ArrayIndexOutOfBoundsException/外層try-catch
當有多個巢狀的try…catch…finally時,要特別注意finally的執行時機。 請先閱讀 EmbedFinally.java示例,再執行它,觀察其輸出並進行總結。
特別注意: 當有多層巢狀的finally時,異常在不同的層次丟擲 ,在不同的位置丟擲,可能會導致不同的finally語句塊執行順序。
public class EmbededFinally { public static void main(String args[]) { int result; try { System.out.println("in Level 1"); try { System.out.println("in Level 2"); // result=100/0; //Level 2 try { System.out.println("in Level 3"); result=100/0; //Level 3 } catch (Exception e) { System.out.println("Level 3:" + e.getClass().toString()); } finally { System.out.println("In Level 3 finally"); } // result=100/0; //Level 2 } catch (Exception e) { System.out.println("Level 2:" + e.getClass().toString()); } finally { System.out.println("In Level 2 finally"); } // result = 100 / 0; //level 1 } catch (Exception e) { System.out.println("Level 1:" + e.getClass().toString()); } finally { . System.out.println("In Level 1 finally"); } } }EmbededFinally .java
執行截圖:
總結:
只有在try語句塊的程式碼中發生錯誤才會被catch語句塊中的程式碼捕獲並處理。不論是否有異常發生,finally語句塊中的語句始終保證被執行。
辨析:finally語句塊一定會執行嗎?
請通過 SystemExitAndFinally.java示例程式回答上述問題
public class SystemExitAndFinally { public static void main(String[] args) { try{ System.out.println("in main"); throw new Exception("Exception is thrown in main"); //System.exit(0); } catch(Exception e) { System.out.println(e.getMessage()); System.exit(0); } finally { System.out.println("in finally"); } } }SystemExitAndFinally
執行截圖:
當程式提前結束時,finally語句塊就不會執行。