Java 简明教程

Java Multiple Catch Blocks

Multiple Catch Blocks in Java

Java 中的多个 catch 块用于捕获/处理可能从特定代码段引发的多个 exceptionstry block 可以有多个 catch 块来处理多个异常。

Multiple catch blocks in Java are used to catch/handle multiple exceptions that may be thrown from a particular code section. A try block can have multiple catch blocks to handle multiple exceptions.

Syntax: Multiple Catch Blocks

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,在这种情况下,当前方法停止执行并且异常被抛到调用堆栈上先前的那个方法。

The previous statements demonstrate three catch blocks, but you can have any number of them after a single try. If an exception occurs in the protected code, the exception is thrown to the first catch block in the list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes down to the second catch statement. This continues until the exception either is caught or falls through all catches, in which case the current method stops execution and the exception is thrown down to the previous method on the call stack.

Points to Remember while using Multiple Catch Blocks

  1. Only one type of exception can be handled at a time. In protected, only one type of exception will be raised, so it will be handled in relevant catch block only.

  2. Order of catch block is very important. Order should be from specific exception to generic one. In case of parent exception block comes before the child exception block, compiler will complain and will throw compile time error.

Example of Java Multiple Catch Blocks

此处是如何使用多个 try/catch 语句的代码片段。在此示例中,我们正在通过将一个值除以 0 来创建一个错误。由于该错误并不是一个 ArrayIndexOutOfBoundsException,所以下一个 catch 块处理该异常并打印该详细信息。

Here is code segment showing how to use multiple try/catch statements. In this example, we’re creating an error by dividing a value by 0. As it is not an ArrayIndexOutOfBoundsException, next catch block handles the exception and prints the details.

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

Handling Multiple Exceptions Using Multiple Catch Blocks

在此代码片段中,我们展示了如何使用另一个多个 try/catch 语句的示例。在此示例中,我们正在通过将一个值除以 0 来创建一个错误并利用 ArithmeticException 处理该错误。

In this code segment, we’re showing how to use another example of multiple try/catch statements. In this example, we’re creating an error by dividing a value by 0 and handling it using ArithmeticException.

Example

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

Handling Multiple Exceptions Within A Single Catch Block

自 Java 7 起,你可以使用一个 catch 块处理多个异常,这个功能简化了代码。以下是处理它的方法 −

Since Java 7, you can handle more than one exception using a single catch block, this feature simplifies the code. Here is how you would do it −

Syntax

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

Example

以下代码片段展示了如何在单一语句中使用多个 catch。在这个例子中,我们通过将一个值除以 0 来创建一个错误。在单一语句中,我们处理 ArrayIndexOutOfBoundsException 和 ArithmeticException。

Here is code segment showing how to use multiple catch in a single statement. In this example, we’re creating an error by dividing a value by 0. In single statement, we’re handling ArrayIndexOutOfBoundsException and 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