Java中全部異常的父類是Throwable類,在Throwable類下有兩大子類:java
一個是Error類,指系統錯誤異常,例如:VirtualMachineError 虛擬機錯誤,ThreadDeath 線程死鎖。通常若是是Error類的異常的話,就是程序的硬傷,就比如是工廠裏斷水斷電,機器損壞了。數據庫
另外一個是Exception類,指編碼、環境、用戶操做輸入等異常,這個是比較常見的異常類,Exception類下面又有兩個子類,RuntimeException 非檢查異常和檢查異常,非檢查又稱爲運行時異常,在RuntimeException異常中有幾個常見的子類,例如:數組
InputMismatchException 輸入不匹配異常學習
ArithmeticException 算術運算異常編碼
NullPointerException 空指針異常spa
ArrayIndexOutOfBoundsException 數組下標越界異常線程
ClassCastException 類型轉換異常指針
檢查異常中的子類有:code
IOException 文件異常blog
SQLException SQL數據庫錯誤異常
在實際的開發中,處理異常通常使用如下三種方式:
1、使用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 }
2、使用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 }
3、自定義異常類
有的時候咱們拋出的異常在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異常處理實際應用中的經驗與總結:
一、處理運行時異常時,採用邏輯去合理規避同時輔助try-catch處理;
二、在多重catch塊後面,能夠加一個catch(Exception)來處理可能會被遺漏的異常;
三、對於不肯定的代碼,也能夠加上try-catch,處理潛在異常;
四、儘可能去處理異常,切忌只是簡單的調用printStackTrace()去打印輸出;
五、具體如何處理異常,要根據不一樣的業務需求和異常類型去決定;
六、儘可能添加finally語句塊去釋放佔用的資源。
注:以上內容是我的學習和工做中的總結,若有不完善和須要改正的地方,能夠留言,謝謝閱讀!