1. 程式人生 > >Java基礎(三十一)

Java基礎(三十一)

異常的捕獲與處理

Java語言提供的最為強大的支援就在於異常的處理操作上

1:什麼是異常

異常是導致程式中斷執行的一種指令流。

下面觀察沒有異常產生的程式執行結果。

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		System.out.println("【2】數學計算"+(10/2));
		System.out.println("【3】程式執行完畢");
		
	}
}
【1】程式開始執行
【2】數學計算5
【3】程式執行完畢

上述程式會按照既定的結構從頭到尾開始執行。

下面觀察產生異常的過程,

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		System.out.println("【2】數學計算"+(10/0));
		System.out.println("【3】程式執行完畢");
		
	}
}

//【1】程式開始執行
//Exception in thread "main" java.lang.ArithmeticException: / by zero
//	at 開始面試.Test.main(Test.java:8)

上述程式出現錯誤之後,整個程式將不會按照特定的方式進行執行,而是中斷了執行;那麼為了保證程式出現了非致命錯誤之後程式依然可以正常完成,所以就需要有一個完善的異常處理機制,以保證程式的順利執行。

2處理異常

在這裡插入圖片描述

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		try {
			System.out.println("【2】數學計算"+(10/0));
		}catch(ArithmeticException e ) {
			System.out.println("處理異常"+e);//異常處理
		}
		System.out.println("【3】程式執行完畢");
		
	}
}

//【1】程式開始執行
//處理異常java.lang.ArithmeticException: / by zero
//【3】程式執行完畢

上述程式碼中,此時發現現在即便出現了異常,程式也可以正常的執行完畢,所以此時的設計屬於一個合理設計。

3:常類中提供的printStackTrace()方法。

但是有一個問題出現了:此時在進行異常處理的時候直接輸出的是一個異常類的物件,那麼對於此物件如果直接列印(上述程式碼中呼叫toString())所得到的異常資訊並不完整,那麼如果要想獲得非常完整的異常資訊,則可以使用異常類中提供的printStackTrace()方法。(觀察如下程式碼)

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		try {
			System.out.println("【2】數學計算"+(10/0));
		}catch(ArithmeticException e ) {
			e.printStackTrace();
		}
		System.out.println("【3】程式執行完畢");
		
	}
}

//【1】程式開始執行
//java.lang.ArithmeticException: / by zero
//【3】程式執行完畢
// 	at 開始面試.Test.main(Test.java:9)

4:finally程式塊

對於異常的處理格式也可以在最後追加一個finally程式快,表示異常處理後的出口,不管是否出現異常執行。

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		try {
			System.out.println("【2】數學計算"+(10/0));
		}catch(ArithmeticException e ) {
			e.printStackTrace();
		}finally {
			System.out.println("【F】不管是否出現異常,我都會執行");
		}	
		System.out.println("【3】程式執行完畢");	
	}
}

//【1】程式開始執行
//【F】不管是否出現異常,我都會執行
//【3】程式執行完畢java.lang.ArithmeticException: / by zero
//
//	at 開始面試.Test.main(Test.java:9)

5:處理多個異常

很多時候在程式執行的過程中可能會產生若干個異常。那麼這種情況下也可以使用多個catch進行異常的捕獲。

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		try {
			int x = Integer.parseInt(args[0]);
			int y = Integer.parseInt(args[1]);
			System.out.println("【2】數學計算"+(x/y));
		}catch(ArithmeticException e ) {
			e.printStackTrace();
		}finally {
			System.out.println("【F】不管是否出現異常,我都會執行");
		}	
		System.out.println("【3】程式執行完畢");	
	}
}

上述的程式碼可能產生三類異常。

1未處理:程式執行的時候沒有輸入初始化引數: 在這裡插入圖片描述

2未處理:輸入的資料不是數字: 在這裡插入圖片描述 3已處理:輸入的被除數為0,那麼程式也會導致中斷(finally程式碼依舊執行),所以在這樣的情況下就必須進行多個異常的捕獲。

修改程式碼如下:

public class Test{
	public static void main(String args[]) {
		System.out.println("【1】程式開始執行");
		try {
			int x = Integer.parseInt(args[0]);
			int y = Integer.parseInt(args[1]);
			System.out.println("【2】數學計算"+(x/y));
		}catch(ArithmeticException e ) {
			e.printStackTrace();
		}catch(NumberFormatException e) {
			e.printStackTrace();
		}catch(ArrayIndexOutOfBoundsException e) {
			e.printStackTrace();
		}
		finally {
			System.out.println("【F】不管是否出現異常,我都會執行");
		}	
		System.out.println("【3】程式執行完畢");	
	}
}