動手動腦5
阿新 • • 發佈:2018-11-11
1.AboutException.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!"); } 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"); } } }
輸出結果:
java.lang.ArithmeticException指算術上的錯誤,將j值改變
使用異常處理後:
2.多層的異常捕獲
public class CatchWho { public static void main(String[] args) { try { try { throw 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"); } } }
輸出結果:
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"); } } }
輸出結果:
當多層巢狀時,通過catch可以捕捉相應異常,但異常不符合指定內容無法捕捉
3.多層巢狀執行
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"); } } }
輸出結果:
當有多層巢狀的finally時,異常在不同的層次丟擲 ,在不同的位置丟擲,可能會導致不同的finally語句塊執行順序。
4.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"); } } }
輸出結果:
finally語句塊不一定執行。
通過System.exit(0)停止程式。
5.PrintExceptionStack.java
public class PrintExceptionStack { public static void main( String args[] ) { try { method1(); } catch ( Exception e ) { System.err.println( e.getMessage() + "\n" ); e.printStackTrace(); } } public static void method1() throws Exception { method2(); } public static void method2() throws Exception { method3(); } public static void method3() throws Exception { throw new Exception( "Exception thrown in method3" ); } }
當程式中出現異常時,JVM會依據方法呼叫順序依次查詢有關的錯誤處理程式。可使用printStackTrace 和 getMessage方法瞭解異常發生的情況:
printStackTrace:列印方法呼叫堆疊。
每個Throwable類的物件都有一個getMessage方法,它返回一個字串,這個字串是在Exception建構函式中傳入的,通常讓這一字串包含特定異常的相關資訊
6.ThrowMultiExceptionsDemo.java
import java.io.*; public class ThrowMultiExceptionsDemo { public static void main(String[] args) { try { throwsTest(); } catch(IOException e) { System.out.println("捕捉異常"); } } private static void throwsTest() throws ArithmeticException,IOException { System.out.println("這只是一個測試"); // 程式處理過程假設發生異常 throw new IOException(); //throw new ArithmeticException(); } }
當一個方法宣告出現多個異常時,在此方法呼叫語句處只要catch捕捉到任何一個異常,程式就可以順利編譯。
8.OverrideThrows.java
import java.io.*; public class OverrideThrows { public void test()throws IOException { FileInputStream fis = new FileInputStream("a.txt"); } } class Sub extends OverrideThrows { //如果test方法宣告丟擲了比父類方法更大的異常,比如Exception //則程式碼將無法編譯…… public void test() throws FileNotFoundException { //... } }
一個子類的throws子句丟擲的異常,不能是其基類同名方法丟擲的異常物件的父類。