日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java如何捕获多个异常_是否可以在单个catch块中捕获多个Java异常?

發布時間:2024/1/23 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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异常?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。