Java 简明教程

Java - Nested Try Block

Nested Try Block

try block 可嵌套在另一个尝试块中。此结构被称为嵌套尝试块。每当 exception 在嵌套尝试块中引发时,其异常将被推送到堆栈中。异常从子尝试块传播到父尝试块,依此类推。

A try block can be nested within another try block. This structure is termed as Nested try block. Whenever an exception is raised within a nested try block, its exception is pushed to Stack. The exception propagates from child to parent try block and so on.

Syntax

嵌套 catch 块的语法如下所示 −

The syntax for nested catch blocks looks like the following −

try { // parent try block
   try {  // child try block

   }
   catch(ExceptionType1 e1){  // child catch block

   }
} catch (ExceptionType2 e1) { // parent catch block

}

前面的语句演示了两个 try/catch 块,但你可以使用任意多个此类块。如果在受保护的子代码中发生异常,则异常会抛出到子列表的 catch 块中。如果抛出异常的数据类型与 ExceptionType1 匹配,则异常会捕获在该处。如果不匹配,则异常会传递到父 catch 语句。这会持续进行,直到异常被捕获或者通过所有 catch 语句,在这种情况下,当前方法停止执行,并且异常会抛出到调用堆栈上先前的那个方法。

The previous statements demonstrate two try/catch blocks, but you can have any number of them. If an exception occurs in the protected child code, the exception is thrown to the catch block of the child list. If the data type of the exception thrown matches ExceptionType1, it gets caught there. If not, the exception passes up to the parent 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.

Pointer To Remember While Using Nested Try Block

  1. Child catch block should have specific exception for better code clarity. Parent catch block can have more generic exception handled so that if child catch block is not able to handle the exception then parent catch block can handle it.

  2. There in no restriction on exception hiearchy to be used in child vs parent catch block.

  3. If a exception is handled correctly in child catch block, then in parent, another exception can be raised and handled.

Java Nested Try Block Example

以下代码段展示了如何使用嵌套 try/catch 语句。在此示例中,我们在嵌套 try 块中创建了一个错误,即对一个值除以 0。子 catch 块会处理此异常并打印它。现在,在父 try 块中,我们再次创建了一个错误,即在访问数组元素时使用无效的数组索引,并且引发了异常。

Here is code segment showing how to use nested try/catch statements. In this example, we’re creating an error by dividing a value by 0 in a nested try block. The child catch block is handling the exception and printing the same. Now in parent try block, we’re again creating an error by using an invalid array index while accessing array elements and exception is raised.

package com.tutorialspoint;

public class ExcepTest {

   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         try {
            int b = 0;
            int c = 1/b;
         }catch(Exception e) {
            System.out.println("Exception thrown: " + e);
         }
         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.ArithmeticException: / by zero
Exception thrown: java.lang.ArrayIndexOutOfBoundsException: 3
Out of the block

More Examples

Example 1

在此代码段中,我们展示了嵌套 try/catch 语句的另一个示例。在此示例中,我们在嵌套 try 块中创建了一个错误,即对一个值除以 0,但我们没有在对应的 catch 块中处理它。由于父 try 块正把引发的异常作为通用的异常进行处理,因此,它捕获子 catch 块引发的异常并打印它。

In this code segment, we’re showing how to use another example of nested try/catch statements. In this example, we’re creating an error by dividing a value by 0 in nested try block but we’re not handling in corresponding catch block. As parent try block is handling the exception raised as generic exception, it captures the exception raised by child catch block and prints the same.

package com.tutorialspoint;

public class ExcepTest {
   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         try {
            int b = 0;
            int c = 1/b;
         }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception thrown: " + e);
         }
         System.out.println("Access element three :" + a[3]);
      }
      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

Example 2

在此代码段中,我们展示了嵌套 try/catch 语句的另一个示例,其中没有 catch 块处理异常。在此示例中,我们在嵌套 try 块中创建了一个错误,即对一个值除以 0,但我们没有在任何 catch 块中处理这种异常。现在,JVM 会捕获该异常并终止该程序,而不打印最后一条语句。

In this code segment, we’re showing the case of nested try/catch statements where exceptions are not handled by any of the catch block. In this example, we’re creating an error by dividing a value by 0 in nested try block but we’re not handling this kind of exception in any catch block. Now JVM will capture the exception and terminate the program without printing the last statement.

package com.tutorialspoint;

public class ExcepTest {
   public static void main(String args[]) {
      try {
         int a[] = new int[2];
         try {
            int b = 0;
            int c = 1/b;
         }catch(ArrayIndexOutOfBoundsException e) {
            System.out.println("Exception thrown: " + e);
         }
         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 in thread "main" java.lang.ArithmeticException: / by zero
	at com.tutorialspoint.ExcepTest.main(ExcepTest.java:10)