Java 教程

Java 控制語句

面向物件程式設計

Java 內建類

Java 檔案處理

Java 錯誤與異常

Java 多執行緒

Java 同步

Java 網路程式設計

Java 集合

Java 介面

Java 資料結構

Java 集合演算法

高階Java

Java 其他

Java APIs與框架

Java 類引用

Java 有用資源

Java 多重catch塊



Java中的多重catch塊

Java中的多重catch塊用於捕獲/處理可能從特定程式碼段丟擲的多個異常try塊可以有多個catch塊來處理多個異常。

語法:多重catch塊

try {
   // Protected code
} catch (ExceptionType1 e1) {
   // Catch block
} catch (ExceptionType2 e2) {
   // Catch block
} catch (ExceptionType3 e3) {
   // Catch block
}

之前的語句演示了三個catch塊,但在單個try之後可以有任意數量的catch塊。如果在受保護的程式碼中發生異常,則該異常將被拋到列表中的第一個catch塊。如果丟擲的異常的資料型別與ExceptionType1匹配,則會在那裡捕獲它。如果不是,則異常將傳遞到第二個catch語句。這將持續進行,直到異常被捕獲或穿過所有catch,在這種情況下,當前方法將停止執行,並且異常將被拋到呼叫堆疊上的前一個方法。

使用多重catch塊時需要注意的幾點

  • 一次只能處理一種型別的異常。在受保護的程式碼中,只會引發一種型別的異常,因此它將只在相關的catch塊中處理。

  • catch塊的順序非常重要。順序應從特定異常到通用異常。如果父異常塊出現在子異常塊之前,編譯器將報錯並丟擲編譯時錯誤。

Java多重catch塊示例

這是一個程式碼片段,展示瞭如何使用多個try/catch語句。在這個例子中,我們透過將值除以0來建立一個錯誤。因為它不是ArrayIndexOutOfBoundsException,所以下一個catch塊處理異常並列印詳細資訊。

package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         int b = 0;
         int c = 1/b;
         System.out.println("Access element three :" + a[3]);
      }
      catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("ArrayIndexOutOfBoundsException thrown  :" + e);
      }catch (Exception e) {
          System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}

輸出

Exception thrown  :java.lang.ArithmeticException: / by zero
Out of the block

使用多個catch塊處理多個異常

在這個程式碼片段中,我們展示瞭如何使用另一個多重try/catch語句的示例。在這個例子中,我們透過將值除以0來建立一個錯誤,並使用ArithmeticException來處理它。

示例

package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         int b = 0;
         int c = 1/b;
         System.out.println("Access element three :" + a[3]);
      }
      catch (ArithmeticException e) {
         System.out.println("ArithmeticException thrown  :" + e);
      }
      catch (ArrayIndexOutOfBoundsException e) {
         System.out.println("ArrayIndexOutOfBoundsException thrown  :" + e);
      }catch (Exception e) {
          System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}

輸出

ArithmeticException thrown  :java.lang.ArithmeticException: / by zero
Out of the block

在一個catch塊中處理多個異常

從Java 7開始,您可以使用單個catch塊處理多個異常,此功能簡化了程式碼。以下是操作方法:

語法

catch (IOException|FileNotFoundException ex) {
   logger.log(ex);
   throw ex;

示例

這是一個程式碼片段,展示瞭如何在單個語句中使用多個catch。在這個例子中,我們透過將值除以0來建立一個錯誤。在一個語句中,我們處理ArrayIndexOutOfBoundsException和ArithmeticException。

package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         int b = 0;
         int c = 1/b;
         System.out.println("Access element three :" + a[3]);
      }
      catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
         System.out.println("Exception thrown  :" + e);
      }
      System.out.println("Out of the block");
   }
}

輸出

Exception thrown  :java.lang.ArithmeticException: / by zero
Out of the block
廣告