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
广告
© . All rights reserved.