java如何捕获多个异常_是否可以在单个catch块中捕获多个Java异常?
例外是程序執行期間發生的問題(運行時錯誤)。發生異常時,程序會突然終止,并且生成異常的行之后的代碼將永遠不會執行。
代碼中有多個異常
在Java 7之前,只要我們有一個可能會生成多個異常的代碼,并且如果您需要專門處理該代碼,則應在一次嘗試中使用多個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");
}
}
}
輸出1Enter?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
輸出2Enter?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
多捕獲塊
從Java 7開始,使用此功能引入了Multicatch塊,您可以在單個catch塊中處理多個異常。
在此,您需要指定所有要處理的異常類,并用“ | |”分隔。”,如下所示-catch(ArrayIndexOutOfBoundsException?|?ArithmeticException?exp)?{
System.out.println("Warning:?Enter?inputs?as?per?instructions?");
}
示例
以下Java程序演示了Multicatch的用法。在這里,我們在單個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?");
}
}
}
輸出1Enter?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
輸出2Enter?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
總結
以上是生活随笔為你收集整理的java如何捕获多个异常_是否可以在单个catch块中捕获多个Java异常?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: idea改成c盘_解决IDEA占用C盘空
- 下一篇: java美元兑换,(Java实现) 美元