Java中所有異常的父類是Throwable類,在Throwable類下有兩大子類:
一個是Error類,指系統錯誤異常,例如:VirtualMachineError 虛擬機錯誤,ThreadDeath 線程死鎖。一般如果是Error類的異常的話,就是程序的硬傷,就好比是工廠里斷水斷電,機器損壞了。
另一個是Exception類,指編碼、環境、用戶操作輸入等異常,這個是比較常見的異常類,Exception類下面又有兩個子類,RuntimeException 非檢查異常和檢查異常,非檢查又稱為運行時異常,在RuntimeException異常中有幾個常見的子類,例如:
InputMismatchException 輸入不匹配異常
ArithmeticException 算術運算異常
NullPointerException 空指針異常
ArrayIndexOutOfBoundsException 數組下標越界異常
ClassCastException 類型轉換異常
檢查異常中的子類有:
IOException 文件異常
SQLException SQL數據庫錯誤異常
在實際的開發中,處理異常一般使用以下三種方式:
一、使用try-catch語句塊捕獲和處理異常
使用try-catch 以及 try-catch-finally 來捕獲和處理異常時,catch里的異常列表一般是子類在前,父類在后,不然編譯時程序會報錯。示例如下:
1 import java.util.InputMismatchException; 2 import java.util.Scanner; 3 4 public class 異常處理 { 5 6 public static void main(String[] args) { 7 8 System.out.println("請輸入你的年齡"); 9 Scanner input = new Scanner(System.in); 10 try{ 11 System.out.println("請輸入第一個數:"); 12 int one = input.nextInt(); 13 System.out.println("請輸入第二個數:"); 14 int two = input.nextInt(); 15 System.out.println("兩數相除結果為:"+one/two); 16 }catch(InputMismatchException e){ 17 System.out.println("請輸入整數"); 18 }catch(ArithmeticException e){ 19 System.out.println("除數不能為零"); 20 }catch(Exception e){ 21 System.out.println("程序執行中出現異常"); 22 }finally{ 23 System.out.println("程序執行結束!"); 24 } 25 26 27 28 } 29 30 }
二、使用throws關鍵字聲明將要拋出何種類型的異常
語法
public void 方法嗎(參數)throws 異常列表{ throw new Exception(); }
示例如下:
1 public class ThrowDemo { 2 3 public static void main(String[] args) { 4 5 ThrowDemo td = new ThrowDemo(); 6 try { 7 td.test(10, 0); 8 } catch (Exception e) { 9 System.out.println("異常拋出"); 10 } 11 } 12 13 public void test(int a,int b) throws Exception{ 14 int c = a/b; 15 System.out.println("計算結果為:"+c); 16 17 } 18 19 }
三、自定義異常類
有的時候我們拋出的異常在Throwable類中沒有定義,就需要我們自己自定義一個異常的類,比如我們實際開發中需要用到一個“開車別喝酒”的異常,我們就可以定義一個這樣的異常類來處理我們項目中需要處理的異常。
自定義異常類的語法:
class 自定義異常類 extends 異常類型{}
自定義異常類需要繼承和它類型相近的Throwable類里面的子類,或者是我們直接讓自定義異常類繼承Exception類,示例如下:
1 /** 2 * 自定義一個異常類 3 * @author lenovo 4 * 5 */ 6 public class MyThrow extends Exception{ 7 8 public MyThrow(){ 9 10 } 11 12 public MyThrow(String mess){ 13 super(mess); 14 } 15 }
使用這個異常類的示例如下:
1 public class ChainTest { 2 3 /** 4 * test1():拋出"喝大了"異常; 5 * test2():調用test1(),捕獲"喝大了"異常,並且包裝成運行時異常,繼續拋出; 6 * main方法中調用test2(),嘗試捕獲test2()方法拋出的異常 7 */ 8 9 public static void main(String[] args) { 10 ChainTest ct = new ChainTest(); 11 ct.test2(); 12 } 13 14 public void test1() throws MyThrow{ 15 throw new MyThrow("喝酒別開車!"); 16 } 17 18 public void test2(){ 19 try { 20 test1(); 21 } catch (MyThrow e) { 22 RuntimeException newExc = new RuntimeException("司機一滴酒,親人兩行淚~~"); 23 newExc.initCause(e); 24 throw newExc; 25 } 26 } 27 28 }
運行結果:
Exception in thread "main" java.lang.RuntimeException: 司機一滴酒,親人兩行淚~~ at xbw.ChainTest.test2(ChainTest.java:24) at xbw.ChainTest.main(ChainTest.java:13) Caused by: xbw.MyThrow: 喝酒別開車! at xbw.ChainTest.test1(ChainTest.java:17) at xbw.ChainTest.test2(ChainTest.java:22) ... 1 more
Java異常處理實際應用中的經驗與總結:
1、處理運行時異常時,采用邏輯去合理規避同時輔助try-catch處理;
2、在多重catch塊后面,可以加一個catch(Exception)來處理可能會被遺漏的異常;
3、對於不確定的代碼,也可以加上try-catch,處理潛在異常;
4、盡量去處理異常,切忌只是簡單的調用printStackTrace()去打印輸出;
5、具體如何處理異常,要根據不同的業務需求和異常類型去決定;
6、盡量添加finally語句塊去釋放占用的資源。