多重catch塊情況下的異常層次結構。
異常是在程式執行期間發生的錯誤(執行時錯誤)。當發生異常時,程式會突然終止,並且異常行之後的程式碼永遠不會執行。
程式碼中的多個異常
當我們的程式碼可能產生多個異常,並且需要分別處理它們時,可以在單個try塊上使用多個catch塊。
try{
//code
} catch(Exception1 ex1) {
//
} catch(Exception2 ex2) {
//
}示例
import java.util.Arrays;
import java.util.Scanner;
public class MultipleCatchBlocks {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (not 0) from this array (enter positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Warning: You have chosen a position which is not in the array");
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
}
}輸出1
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator(not 0) from this array (enter positions 0 to 5) 2 8 Warning: You have chosen a position which is not in the array
輸出2
Enter 3 integer values one by one: Array: [10, 20, 30, 2, 0, 8] Choose numerator and denominator (not 0) from this array (enter positions 0 to 5) 1 4 Warning: You cannot divide a number with 0
異常順序
如果對於單個try塊,您有多個catch塊,並且它們的異常類屬於同一個層次結構,則需要確保捕獲較高級別異常類的catch塊最後放置。
否則,所有子類的異常都將在較高級別的catch塊中處理,導致其餘(catch)塊無法訪問,從而導致編譯時異常。
示例
在以下Java程式中,單個try塊包含4個catch塊,依次處理IndexOutOfBoundsException、ArrayIndexOutOfBoundsException、Exception和ArithmeticException。
由於IndexOutOfBoundsException是ArrayIndexOutOfBoundsException的超類,因此與這兩個類相關的異常都將在第一個catch塊中處理,導致第二個catch塊無法訪問。
由於Exception是所有異常類的超類,如果您將捕獲它的catch塊放在捕獲任何其他異常的catch塊之前,則所有異常都將在Exception塊本身中處理,導致其餘塊無法訪問。
import java.util.Arrays;
import java.util.Scanner;
public class ExceptionHierarchy {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(IndexOutOfBoundsException e) {
System.out.println("Warning: position chosen is not in the array");
}
catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
catch(Exception e) {
e.printStackTrace();
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
}
}輸出
編譯時異常
ExceptionHierarchy.java:16: error: exception ArrayIndexOutOfBoundsException has already been caught
}catch(ArrayIndexOutOfBoundsException e) {
^
ExceptionHierarchy.java:20: error: exception ArithmeticException has already been caught
}catch(ArithmeticException e) {
^
2 errors如果您在eclipse中編譯上述程式碼,它會生成以下錯誤:
訊息1 −

訊息2 −

解決方案
要使上述程式碼正常工作,
捕獲IndexOutOfBoundsException的catch塊應該放在捕獲ArrayIndexOutOfBoundsException的catch塊之後。
捕獲Exception物件的catch塊應該放在catch塊順序的最後。
示例
import java.util.Arrays;
import java.util.Scanner;
public class ExceptionHierarchy {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Array: "+Arrays.toString(arr));
System.out.println("Choose numerator and denominator (enter positions 0 to 5)");
int a = sc.nextInt();
int b = sc.nextInt();
try {
int result = (arr[a])/(arr[b]);
System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result);
}
catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
catch(IndexOutOfBoundsException e) {
System.out.println("Warning: You have chosen a position which is not in the array");
}
catch(ArithmeticException e) {
System.out.println("Warning: You cannot divide a number with 0");
}
catch(Exception e) {
e.printStackTrace();
}
}
}
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP