能否在一個catch塊中捕獲多個Java異常?
異常是在程式執行期間發生的錯誤(*執行時錯誤*)。當發生異常時,程式會突然終止,異常行之後的程式碼將不會執行。
程式碼中的多個異常
在Java 7之前,如果程式碼可能產生多個異常,並且需要分別處理它們,則必須在單個try塊上使用多個catch塊。
示例
下面的Java程式包含一個數字陣列(已顯示)。它從使用者那裡接收陣列中的兩個位置,並將第一個位置的數字除以第二個位置的數字。
輸入值時:
如果選擇的位置不在顯示的陣列中,則會丟擲ArrayIndexOutOfBoundsException異常。
- 如果選擇0作為分母,則會丟擲ArithmeticException異常。
在這個程式中,我們使用兩個不同的catch塊處理了所有可能的異常。
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("Enter 3 integer values one by one: ");
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
多重catch塊
從Java 7開始,引入了多重catch塊,可以使用它在一個catch塊中處理多個異常。
在這種情況下,需要使用“|”分隔要處理的所有異常類,如下所示:
catch(ArrayIndexOutOfBoundsException | ArithmeticException exp) {
System.out.println("Warning: Enter inputs as per instructions ");
}示例
下面的Java程式演示了多重catch塊的使用。在這裡,我們在一個catch塊中處理所有異常。
import java.util.Arrays;
import java.util.Scanner;
public class MultiCatch {
public static void main(String [] args) {
Scanner sc = new Scanner(System.in);
int[] arr = {10, 20, 30, 2, 0, 8};
System.out.println("Enter 3 integer values one by one: ");
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 | ArithmeticException exp) {
System.out.println("Warning: Enter inputs as per instructions ");
}
}
}輸出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) 0 9 Warning: Enter inputs as per instructions
輸出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) 2 4 Warning: Enter inputs as per instructions
廣告
資料結構
網路
關係資料庫管理系統(RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP