大二上第九周作業
阿新 • • 發佈:2018-11-11
一、多層異常捕獲
1 import javax.swing.*; 2 3 class AboutException { 4 public static void main(String[] a) 5 { 6 int i=1, j=0, k; 7 // k=i/j; 8 9 10 try 11 { 12 13 k = i/j; // Causes division-by-zero exception 14 //throw new Exception("Hello.Exception!");AboutException.java15 } 16 17 catch ( ArithmeticException e) 18 { 19 System.out.println("被0除. "+ e.getMessage()); 20 } 21 22 catch (Exception e) 23 { 24 if (e instanceof ArithmeticException) 25 System.out.println("被0除"); 26 else 27 { 28 System.out.println(e.getMessage());29 30 } 31 } 32 33 34 finally 35 { 36 JOptionPane.showConfirmDialog(null,"OK"); 37 } 38 39 } 40 }
1.throw語句丟擲異常物件
2.允許執行期間判斷某條件是否滿足,不滿足則丟擲AssertionError.
3.啟用assert功能
二、多層異常捕獲public classCatchWho2CatchWho2 { 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"); } } }
總結:異常在不同的層次丟擲,不同的位置丟擲,會導致不同的finally語句塊執行
三、finally 語句一定會執行嗎?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
System.exit(0)語句會提前終止程式,所以以上程式碼不會執行finally語句。
四、如何跟蹤異常的傳播路徑?// UsingExceptions.java // Demonstrating the getMessage and printStackTrace // methods inherited into all exception classes. 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" ); } }PrintExceptionStack
以上表明:異常的傳播路徑為:8-16-20-24
五、歸納與總結
1.throw語句丟擲異常物件及啟用assert功能
2.多層異常捕獲時語句的執行順序以及如何判斷語句的執行順序
3.傳播異常的跟蹤:物件e.getMassage()及e.printStackTrace()的呼叫
1 package Exception; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 6 public class TestThrows { 7 8 public static void main(String[] args) { 9 // TODO 自動生成的方法存根 10 FileInputStream fis = new FileInputStream("a.txt"); 11 } 12 }
對比於
1 package Exception; 2 3 import java.io.FileInputStream; 4 import java.io.FileNotFoundException; 5 6 public class TestThrows { 7 8 public static void main(String[] args) throws FileNotFoundException 9 { 10 // TODO 自動生成的方法存根 11 FileInputStream fis = new FileInputStream("a.txt"); 12 } 13 }
throws語句表明:某方法可能出現某種異常,但他自己不能處理,需要由呼叫者來處理
六、受控與不受控異常package Exception; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class CheckedExceptionDemo { public static void main(String[] args) { // TODO 自動生成的方法存根 try{ //丟擲受控異常 BufferedReader buf= new BufferedReader(new InputStreamReader(System.in)); System.out.println("請輸入整數:"); int input = Integer.parseInt(buf.readLine());//有可能引發執行時異常 System.out.println("input x 10 = " + (input*10)); } //受控異常 catch(IOException e){ System.out.println("I/O錯誤"); } //非受控異常 catch(NumberFormatException e){ System.out.println("輸入必須為整數"); } } }CheckedExceptionDemo
總結:受控異常需要在catch語句塊中宣告,而不受控異常不需要
七、子類丟擲受控異常的限制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 { //... } }OverrideThrows
總結:一個子類的throws子句丟擲的異常,不能是其基類方法的丟擲異常物件的父類
八、
九、實際開發中的異常處理 自定義異常與異常處理鏈1.自定義異常通常選擇直接派生自Exception
Class MyException extends Exception{ .......}
2.在合適的地方使用throw語句丟擲自定義異常物件
Class MyClass{ void someMethod(){ if(條件)throw new MyException(); } }
1 /** 2 * 自定義的異常類 3 * @author JinXuLiang 4 * 5 */ 6 class MyException extends Exception 7 { 8 public MyException(String Message) { 9 super(Message); 10 } 11 public MyException(String message, Throwable cause) { 12 super(message, cause); 13 } 14 public MyException( Throwable cause) { 15 super(cause); 16 } 17 18 } 19 20 public class ExceptionLinkInRealWorld { 21 public static void main( String args[] ) 22 { 23 try { 24 throwExceptionMethod(); //有可能丟擲異常的方法呼叫 25 } 26 catch ( MyException e ) 27 { 28 System.err.println( e.getMessage() ); 29 System.err.println(e.getCause().getMessage()); 30 } 31 catch ( Exception e ) 32 { 33 System.err.println( "Exception handled in main" ); 34 } 35 doesNotThrowException(); //不丟擲異常的方法呼叫 36 } 37 38 public static void throwExceptionMethod() throws MyException 39 { 40 41 try { 42 System.out.println( "Method throwException" ); 43 44 throw new Exception("系統執行時引發的特定的異常"); // 產生了一個特定的異常 45 } 46 catch( Exception e ) 47 { 48 System.err.println( 49 "Exception handled in method throwException" ); 50 //轉換為一個自定義異常,再丟擲 51 throw new MyException("在方法執行時出現異常",e); 52 53 54 } 55 finally { 56 System.err.println( 57 "Finally executed in throwException" ); 58 } 59 60 // any code here would not be reached 61 } 62 63 public static void doesNotThrowException() 64 { 65 try { 66 System.out.println( "Method doesNotThrowException" ); 67 } 68 catch( Exception e ) 69 { 70 System.err.println( e.toString() ); 71 } 72 finally { 73 System.err.println( 74 "Finally executed in doesNotThrowException" ); 75 } 76 77 System.out.println( 78 "End of method doesNotThrowException" ); 79 } 80 }ExceptionLinkInRealWorld