異常概念和處理機制java
什麼是異常?程序員
所謂異常就是指在程序運行的過程當中發生的一些不正常事件。(如除0溢出,數組下標越界,所要讀取的文件不存在);數組
異常致使的後果?this
Java程序的執行過程當中如出現異常事件,能夠生成一個異常類對象,該異常對象封裝了異常事件的信息,其將被提交給Java運行時系統,這個過程稱爲拋出異常,不處理的話會致使程序直接中斷;spa
如何防止程序中斷?設計
設計良好的程序應該在程序異常發生時提供處理這些異常的方法,使得程序不會由於異常的發生而阻斷或產生不可預見的結果;指針
異常分類code
異常類型orm |
包括對象 |
來源 |
處理 |
受查異常 checkedException |
Exception及其子類(不包括 RuntimeException 及其子類) |
由代碼控制能力以外的因素致使額運行時錯誤 |
必需要處理,不然通不過編譯 |
非受查異常 uncheckedException |
Error和 RuntimeException 及其子類 |
RuntimeException 通常表明編譯錯誤 |
能夠不用處理 |
Java的異常經過兩種機制來處理
捕獲:try-catch-finally
拋出:throw,throws
try-catch-finally
try{}語句塊中放的是要檢測的java代碼,可能有會拋出異常,也可能會正常執行;
catch(異常類型){}塊是當Java運行時系統接收到try塊中所拋出的異常對象時,會尋找能處理這一異常的catch塊來進行處理(能夠有多個catch塊);
finally{}塊無論系統有沒有拋出異常都會去執行,通常用來釋放資源。除了在以前執行了System.exit(0);
1 import java.util.Scanner; 2 import java.util.InputMismatchException; 3 public class ExceptionDemo1{ 4 public static void main(String []args){ 5 System.out.println("請輸入一個數字"); 6 Scanner input=new Scanner(System.in); 7 int res=0; 8 try{ 9 //return;//添加return以後,仍是會執行finally語句塊 10 //System.exit(0);//添加以後不會執行finally語句塊
11 int number=input.nextInt(); 12 res=10/number; 13 }catch(InputMismatchException e){ 14 //輸入字母時錯誤 15 //錯誤信息
16 System.out.println(e.getMessage()); 17 //堆棧信息
18 e.printStackTrace(); 19 }catch(ArithmeticException e){ 20 //輸入0時錯誤 21 //錯誤信息
22 System.out.println(e.getMessage()); 23 //堆棧信息
24 e.printStackTrace(); 25 }catch(Exception e){ 26 //該異常爲父類,若不清楚是何異常,可使用該類 27 //若都使用時,應該先使用子類再用該類
28 System.out.println(e.getMessage()); 29 e.printStackTrace(); 30 } 31 finally{ 32 //釋放資源,好比關閉打開的文件,刪除一些臨時文件等
33 System.out.println("結果爲:"+res); 34 } 35 } 36 }
空指針異常
1 public class ExceptionDemo1{ 2 public static void main(String []args){ 3 //testTryFinally("張三");//輸出2 end
4 testTryFinally(null);//空指針異常
5 } 6
7 public static void testTryFinally(String name){ 8 try{ 9 System.out.println(name.length()); 10 }finally{ 11 System.out.println("end"); 12 } 13 } 14 }
throw和throws
throw用於手動拋出異常。做爲程序員能夠再任意位置手動拋出異常;
throws用於在方法上標識要暴露的異常。拋出的異常交由調用者處理;
二者區別:
① throw用在方法內,後面跟上要拋出的異常類對象;
② throws修飾在方法上,告訴調用者此方法可能會拋出異常,後面跟上要拋出的異常類名;
未處理異常
1 public class ExceptionDemo2{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 bar.enter(15); 5 //未打印end
6 System.out.println("end"); 7 } 8 } 9
10 class Bar{ 11 public void enter(int age){ 12 if(age<18){ 13 //受查異常(必須捕獲,不然編譯不經過)和非受查異常
14 throw new IllegalArgumentException("年齡不合格"); 15 }else{ 16 System.out.println("歡迎光臨"); 17 } 18 } 19 }
在調用方法時處理異常
1 public class ExceptionDemo2{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 try{ 5 bar.enter(15); 6 }catch(IllegalArgumentException e){ 7 System.out.println("錯誤信息:"+e.getMessage()); 8 } 9 System.out.println("end"); 10 } 11 } 12
13 class Bar{ 14 public void enter(int age){ 15 if(age<18){ 16 //受查異常(必須捕獲,不然編譯不經過)和非受查異常
17 throw new IllegalArgumentException("年齡不合格"); 18 }else{ 19 System.out.println("歡迎光臨"); 20 } 21 } 22 }
方法中未處理異常,但調用者可能不知道拋出什麼異常,全部在方法上加上throws,用於方法中未處理異常,交由調用者處理
1 public class ExceptionDemo2{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 try{ 5 bar.enter(15); 6 }catch(IllegalArgumentException e){ 7 System.out.println("錯誤信息:"+e.getMessage()); 8 } 9 System.out.println("end"); 10 } 11 } 12
13 class Bar{ 14 public void enter(int age)throws IllegalArgumentException{ 15 if(age<18){ 16 //受查異常(必須捕獲,不然編譯不經過)和非受查異常
17 throw new IllegalArgumentException("年齡不合格"); 18 }else{ 19 System.out.println("歡迎光臨"); 20 } 21 } 22 }
受查異常必需要捕獲,不然編譯不經過
1 public class ExceptionDemo2{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 //try{
5 bar.enter(15); 6 /*}catch(IllegalArgumentException e){ 7 System.out.println("錯誤信息:"+e.getMessage()); 8 }*/
9 System.out.println("end"); 10 } 11 } 12
13 class Bar{ 14 public void enter(int age) { 15 if(age<18){ 16 //受查異常(必須捕獲,不然編譯不經過)和非受查異常 17 //throw new IllegalArgumentException("年齡不合格");//非受查異常
18 throw new Exception("年齡不合格");//受查異常
19 }else{ 20 System.out.println("歡迎光臨"); 21 } 22 } 23 }
當拋出的是受查異常時,且方法中不本身捕獲異常,必須加上throws,不然編譯不經過
1 public class ExceptionDemo2{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 try{ 5 bar.enter(15); 6 }catch(Exception e){ 7 System.out.println("錯誤信息:"+e.getMessage()); 8 } 9 System.out.println("end"); 10 } 11 } 12
13 class Bar{ 14 public void enter(int age)throws Exception{ 15 if(age<18){ 16 //受查異常(必須捕獲,不然編譯不經過)和非受查異常 17 //throw new IllegalArgumentException("年齡不合格");//非受查異常
18 throw new Exception("年齡不合格");//受查異常
19 }else{ 20 System.out.println("歡迎光臨"); 21 } 22 } 23 }
自定義異常
常見異常:
非受查異常RuntimeException
受查異常IOException,SQLException,ClassNotFoundException
自定義異常:Java提供的異常體系不可能預見全部但願加以報告的錯誤;
自定義異常類必須從已有的異常類繼承:用的最多的是Exception;
創建新的異常類型最簡單的方法就是讓編譯器產生默認構造方法;
對異常來講,最重要的部分就是它的類名;
能夠爲異常類定義一個接受字符串參數的構造方法,字符串參數描述異常信息;
1 public class ExceptionDemo3{ 2 public static void main(String []args){ 3 Bar bar=new Bar(); 4 try{ 5 bar.enter(15); 6 }catch(AgeLessThanEighteenException e){ 7 System.out.println("錯誤信息:"+e.getMessage()); 8 } 9 System.out.println("end"); 10 } 11 } 12
13 //自定義異常
14 class AgeLessThanEighteenException extends Exception{ 15 private String message;//描述異常信息
16 public AgeLessThanEighteenException(String message){ 17 this.message=message; 18 } 19 //重寫getMessage()方法
20 public String getMessage(){ 21 return message; 22 } 23 } 24
25 class Bar{ 26 public void enter(int age)throws AgeLessThanEighteenException{ 27 if(age<18){ 28 throw new AgeLessThanEighteenException("年齡不合格"); 29 }else{ 30 System.out.println("歡迎光臨"); 31 } 32 } 33 }