Java 教程

Java 控制语句

面向对象编程

Java 内置类

Java 文件处理

Java 错误和异常

Java 多线程

Java 同步

Java 网络编程

Java 集合

Java 接口

Java 数据结构

Java 集合算法

高级 Java

Java 杂项

Java API 和框架

Java 类引用

Java 有用资源

Java try-catch 块



一个异常(或异常事件)是在程序执行期间出现的错误。当发生异常时,程序的正常流程会被中断,程序/应用程序会异常终止,这是不推荐的,因此,这些异常需要被处理。

Java trycatch

一个方法使用trycatch 关键字的组合来捕获异常。trycatch 块放置在可能产生异常的代码周围。trycatch 块内的代码称为受保护代码,使用 try/catch 的语法如下所示:

try

容易发生异常的代码放在try块中。当发生异常时,该异常由与其关联的catch块处理。每个 try 块都应该紧跟一个catch块或finally块。

catch

catch 语句包含声明您尝试捕获的异常类型。如果受保护代码中发生异常,则检查 try 后面的 catch 块(或块)。如果发生的异常类型在 catch 块中列出,则异常会被传递到 catch 块,就像参数传递到方法参数一样。

Java try 和 catch 块的语法

try {
   // Protected code
} catch (ExceptionName e1) {
   // Catch block
}

Java try 和 catch 块的示例

在下面的示例中,声明了一个包含 2 个元素的数组。然后代码尝试访问数组的第 3 个元素,这将引发异常。由于我们已将代码用 try 块括起来,因此可以在我们声明的用于捕获 ArrayIndexOutOfBoundsException 的下一个 catch 块中处理此异常。捕获异常后,我们可以采取相应的操作。这里我们打印抛出的异常的详细信息。

package com.tutorialspoint;

public class ExcepTest {

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

输出

Exception thrown  :java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block

使用 Java try 的多个 catch 块

一个 try 块可以后跟多个 catch 块。多个 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块

这是一个代码段,展示了如何使用多个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

使用Java Try和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.