異常:在Java語言中,將程序執行中發生的不正常情況稱為「異常」。 (開發過程中的語法錯誤和邏輯錯誤不是異常)
Java程序在執行過程中所發生的異常事件可分為兩類:
異常的體系結構 * java.lang.Throwable * |-----java.lang.Error:一般不編寫針對性的代碼進行處理。 * |-----java.lang.Exception:可以進行異常的處理 * |------編譯時異常(checked)不會生成字節碼文件 * |-----IOException * |-----FileNotFoundException * |-----ClassNotFoundException * |------運行時異常(unchecked,RuntimeException) * |-----NullPointerException//空指針異常 * |-----ArrayIndexOutOfBoundsException//數組角標越界 * |-----ClassCastException//類型轉化異常 * |-----NumberFormatException//編碼格式異常 * |-----InputMismatchException//輸入不匹配 * |-----ArithmeticException//算術異常
java中異常類的繼承關係
//******************以下是運行時異常*************************** //ArithmeticException @Test public void test6(){ int a = 10; int b = 0; System.out.println(a / b); } //InputMismatchException @Test public void test5(){ Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); System.out.println(score); scanner.close(); } //NumberFormatException @Test public void test4(){ String str = &34;; str = &34;; int num = Integer.parseInt(str); } //ClassCastException @Test public void test3(){ Object obj = new Date(); String str = (String)obj; } //IndexOutOfBoundsException @Test public void test2(){ //ArrayIndexOutOfBoundsException// int[] arr = new int[10];// System.out.println(arr[10]); //StringIndexOutOfBoundsException String str = &34;; System.out.println(str.charAt(3)); } //NullPointerException @Test public void test1(){ // int[] arr = null;// System.out.println(arr[3]); String str = &34;; str = null; System.out.println(str.charAt(0)); } //******************以下是編譯時異常*************************** @Test public void test7(){// File file = new File(&34;);// FileInputStream fis = new FileInputStream(file);// // int data = fis.read();// while(data != -1){// System.out.print((char)data);// data = fis.read();// }// // fis.close(); }
過程一:&34;:程序在正常執行的過程中,一旦出現異常,就會在異常代碼處生成一個對應異常類的對象。並將此對象拋出。一旦拋出對象以後,其後的代碼就不再執行。
關於異常對象的產生:
① 系統自動生成的異常對象
② 手動的生成一個異常對象,並拋出(throw)
過程二:&34;:可以理解為異常的處理方式:① try-catch-finally ② throws
try{ //可能出現異常的代碼 }catch(異常類型1 變量名1){ //處理異常的方式1}catch(異常類型2 變量名2){ //處理異常的方式2}catch(異常類型3 變量名3){ //處理異常的方式3} ....finally{ //一定會執行的代碼}
說明:
如何看待代碼中的編譯時異常和運行時異常?
&34;==寫在方法的聲明處==。指明此方法執行時,可能會拋出的異常類型。 一旦當方法體執行時,出現異常,仍會在異常代碼處生成一個異常類的對象,此對象滿足throws後異常類型時,就會被拋出。異常代碼後續的代碼,就不再執行!
try-catch-finally:真正的將異常給處理掉了。 throws的方式只是將異常拋給了方法的調用者。==並沒真正將異常處理掉==。
補充: 方法重寫的規則之一: 子類重寫的方法拋出的異常類型不大於父類被重寫的方法拋出的異常類型
在程序執行中,除了自動拋出異常對象的情況之外,我們還可以手動的throw一個異常類的對象。
throw 和 throws區別: throw 表示拋出一個異常類的對象,生成異常對象的過程。聲明在方法體內。 throws 屬於異常處理的一種方式,聲明在方法的聲明處。
class Student{ private int id; public void regist(int id) throws Exception { if(id > 0){ this.id = id; }else{ //手動拋出異常對象// throw new RuntimeException(&34;);// throw new Exception(&34;); throw new MyException(&34;); } } @Override public String toString() { return &34; + id + &34;; } }
public class MyException extends Exception{ static final long serialVersionUID = -7034897193246939L; public MyException(){ } public MyException(String msg){ super(msg); }}
轉自:https://my.oschina.net/RealBruce/blog/3218016