Java提供了try(嘗試)、catch(捕捉)、finally(最終)這三個關鍵字來處理異常。在處理各種異常時,需要用到對應的異常類,指的是由程序拋出的對象所屬的類。
一、異常處理的使用
由於finally塊是可以省略的,異常處理格式可以分為三類:try{ }——catch{ }、try{ }——catch{ }——finally{ }、try{ }——finally{ }。
1 public class DealException 2 { 3 public static void main(String args[]) 4 { 5 try 6 //要檢查的程序語句 7 { 8 int a[] = new int[5]; 9 a[10] = 7;//出現異常 10 } 11 catch(ArrayIndexOutOfBoundsException ex) 12 //異常發生時的處理語句 13 { 14 System.out.println("超出數組范圍!"); 15 } 16 finally 17 //這個代碼塊一定會被執行 18 { 19 System.out.println("*****"); 20 } 21 System.out.println("異常處理結束!"); 22 } 23 }
可以看出,在異常捕捉的過程中要進行兩個判斷,第一是try程序塊是否有異常產生,第二是產生的異常是否和catch()括號內想要捕捉的異常相同。
那么,如果出現的異常和catch()內想要捕捉的異常不相同時怎么辦呢?事實上我們可以在一個try語句后跟上多個異常處理catch語句,來處理多種不同類型的異常。
1 public class DealException 2 { 3 public static void main(String args[]) 4 { 5 try 6 //要檢查的程序語句 7 { 8 int a[] = new int[5]; 9 a[0] = 3; 10 a[1] = 1; 11 //a[1] = 0;//除數為0異常 12 //a[10] = 7;//數組下標越界異常 13 int result = a[0]/a[1]; 14 System.out.println(result); 15 } 16 catch(ArrayIndexOutOfBoundsException ex) 17 //異常發生時的處理語句 18 { 19 System.out.println("數組越界異常"); 20 ex.printStackTrace();//顯示異常的堆棧跟蹤信息 21 } 22 catch(ArithmeticException ex) 23 { 24 System.out.println("算術運算異常"); 25 ex.printStackTrace(); 26 } 27 finally 28 //這個代碼塊一定會被執行 29 { 30 System.out.println("finally語句不論是否有異常都會被執行。"); 31 } 32 System.out.println("異常處理結束!"); 33 } 34 }
上述例子中ex.printStackTrace();就是對異常類對象ex的使用,輸出了詳細的異常堆棧跟蹤信息,包括異常的類型,異常發生在哪個包、哪個類、哪個方法以及異常發生的行號。
二、throws關鍵字
throws聲明的方法表示該方法不處理異常,而由系統自動將所捕獲的異常信息拋給上級調用方法。
1 public class throwsDemo 2 { 3 public static void main(String[] args) 4 { 5 int[] a = new int[5]; 6 try 7 { 8 setZero(a,10); 9 } 10 catch(ArrayIndexOutOfBoundsException ex) 11 { 12 System.out.println("數組越界錯誤!"); 13 System.out.println("異常:"+ex); 14 } 15 System.out.println("main()方法結束。"); 16 } 17 private static void setZero(int[] a,int index) throws ArrayIndexOutOfBoundsException 18 { 19 a[index] = 0; 20 } 21 }
throws關鍵字拋出異常,“ArrayIndexOutOfBoundsException”表明setZero()方法可能存在的異常類型,一旦方法出現異常,setZero()方法自己並不處理,而是將異常提交給它的上級調用者main()方法。
三、throw關鍵字
throw的作用是手工拋出異常類的實例化對象。
1 public class throwDemo 2 { 3 public static void main(String[] args) 4 { 5 try 6 { 7 //拋出異常的實例化對象 8 throw new ArrayIndexOutOfBoundsException("\n個性化異常信息:\n數組下標越界"); 9 } 10 catch(ArrayIndexOutOfBoundsException ex) 11 { 12 System.out.println(ex); 13 } 14 } 15 }
我們能發現,throw好像屬於沒事找事,引發運行期異常,並自定義提示信息。事實上,throw通常和throws聯合使用,拋出的是程序中已經產生的異常類實例。

1 public class ExceptionDemo 2 { 3 public static void main(String[] args) 4 { 5 int[] a = new int[5]; 6 try 7 { 8 setZero(a,10); 9 } 10 catch(ArrayIndexOutOfBoundsException e) 11 { 12 System.out.println("異常:"+e); 13 } 14 System.out.println("main()方法結束!"); 15 } 16 public static void setZero(int[] a,int index) throws ArrayIndexOutOfBoundsException 17 { 18 System.out.println("setZero方法開始:"); 19 try 20 { 21 a[index] = 0; 22 } 23 catch(ArrayIndexOutOfBoundsException ex) 24 { 25 throw ex; 26 } 27 finally 28 { 29 System.out.println("setZero方法結束。"); 30 } 31 } 32 }
輸出結果:
setZero方法開始:
setZero方法結束。
異常:java.lang.ArrayIndexOutOfBoundsException: 10
main()方法結束!
四、RuntimeException類
Exception和RuntimeException的區別:
Exception:強制性要求用戶必須處理;
RunException:是Exception的子類,由用戶選擇是否進行處理。
五、自定義異常類
自定義異常類繼承自Exception類,可以使用父類的大量的方法,也可自己編寫方法來處理特定的事件。Java提供用繼承的方式運行用戶自己編寫的異常類。
1 class MyException extends Exception 2 { 3 public MyException(String message) 4 { 5 super(message); 6 } 7 } 8 public class DefinedException 9 { 10 public static void main(String[] args) 11 { 12 try 13 { 14 throw new MyException("\n自定義異常類!"); 15 } 16 catch(MyException e) 17 { 18 System.out.println(e); 19 } 20 } 21 }