多重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();
      }
   }
}

更新於: 2019年9月10日

5K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.