Nested Try in Java | Nested Try Catch Block

When a try block is defined within another try, it is called nested try block in Java. The try block which encloses another try block is called outer try block, and the enclosed try block is called inner try block.

The general syntax of the nested try block is as follows:

try // Outer try block
{
  statements;
  statements;
try // Inner try block
{
  statements;
  statements;
}
catch(Exception1 e1) // Inner catch block
{
  statements;
  statements;
 }
}
catch(Exception2 e2) // Outer catch block
{
  statements;
  statements;
}

In the above syntax, we have considered two try statements for the sake of simplicity and ease of understanding. Here, we have placed one try-catch block inside another try-catch block.

Different Cases Associated with Nested Try Block in Java


Let’s understand the different cases associated with them.


Case 1:

If an exception occurs within the outer try block, the control of execution is transferred from the outer try block to outer catch block that will handle the exception thrown by outer try block.Nested Try block in Java

After handling the exception by catch block, the execution continues with the statement following the outer catch block. The inner try block and its corresponding catch blocks are skipped in this case.

Case 2:

If an exception does not occur inside the outer try block, the control of execution enters into the inner try block. When an exception occurs inside the inner try block, the catch block associated with this inner try block is searched for a proper match.

Java nested try catch block

If a match is found, then the execution of outer catch block is skipped and the execution continues with statements following outer catch block.

If no match is found, the control of execution is transferred to the next outer try-catch block to handle the exception of inner try block. This process continues until and unless an appropriate match is found.

If no match is found, then Java runtime system will handle the exception at runtime and the program terminates abnormally.


Case 3:

If both try blocks do not throw any exception, both catch blocks are skipped naturally and the execution continues with statements following the outer catch block.

Java Nested Try Catch Block Example


Let’s take some example programs based on Java nested try catch statement.

Example 1:

package nestedTryExample;
public class NestedTryBlockEx1 {
public static void main(String[] args) 
{ 
  String str = "Scientech Easy"; 
  int x[ ] = {0, 1, 2, 3, 4}; 

  try // Outer try block 
  { 
    str = null; // Exception occurred. 
    int slength = str.length(); 
    System.out.println("String length: " +slength); 

    try // Inner try block 
    { 
      int y = 6; 
      System.out.println(x[y]); 
    } 
 // Inner catch block.
    catch(ArrayIndexOutOfBoundsException aie) 
    {
      System.out.println("Exception is thrown"); 
      System.out.println(aie.toString()); 
    } 
  } // Outer try block ends here.
// Outer catch block.
   catch(NullPointerException npe) 
   { 
     System.out.println("Exception is thrown "); 
     System.out.println(npe.toString()); 
   } 
 } 
}
Output: 
      Exception is thrown java.lang.NullPointerException

In the above code, we have defined a try-catch block inside the try block. An exception occurred inside the outer try block. So, the control of execution passes to the outer catch block to handle exception without execution of the inner try-catch block.

Example 2:

package nestedTryExample;
public class NestedTryBlockEx2 
{
public static void main(String[] args) 
{ 
  String str = "Scientech Easy"; 
  int x[ ] = {0, 1, 2, 3, 4}; 

 try // Outer try block 
 { 
   int slength = str.length(); 
   System.out.println("String length: " +slength); 

   try // Inner try block 
   { 
     int y = 6; 
     System.out.println(x[y]); // Exception occurred. 
   } 
   catch(ArrayIndexOutOfBoundsException aie) 
   { 
      System.out.println("Exception is thrown"); 
      System.out.println(aie.toString()); 
    } 
  } // Outer try block ends here.
  catch(NullPointerException npe) 
  { 
     System.out.println("Exception is thrown "); 
     System.out.println(npe.toString()); 
  } 
 } 
}
Output: 
       String length: 14 
       Exception is thrown java.lang.ArrayIndexOutOfBoundsException: 6

In the preceding code, we have defined a try-catch block inside a try block. The outer try block did not throw any exception but an exception occurs inside the inner try block. So, the control is transferred to the inner catch block to handle the exception. Since a match is found, the execution of outer catch block is skipped.

Example 3:

package nestedTryExample;
public class NestedTryBlockEx3 {
public static void main(String[] args) 
{ 
  String str = "Scientech Easy"; 
  int x[ ] = {0, 1, 2, 3, 4}; 

  try // Outer try block 
  { 
    int slength = str.length(); 
    System.out.println("String length: " +slength); 

    try // Inner try block 
    { 
      int y = 6; 
      System.out.println(x[y]); // Exception occurred. 
    } 
    catch(ArithmeticException ae) // No match is found. 
    { 
      System.out.println("Exception is thrown"); 
      System.out.println(ae.toString()); 
    } 
  } 
  catch(ArrayIndexOutOfBoundsException aie) // Match found. 
  { 
     System.out.println("Exception is thrown "); 
     System.out.println(aie.toString()); 
  } 
  System.out.println("I am out of outer catch block"); 
 } 
}
Output: 
      String length: 14 
      Exception is thrown java.lang.ArrayIndexOutOfBoundsException: 6 
      I am out of outer catch block

In this example program, an exception is thrown by inner try block but it could not be handled by inner catch block because the type of exception defined by inner catch does not match with the exception thrown by inner try block. In this case, the program control is passed to the outer catch block to handle the exception thrown by inner block.

Example 4:

package nestedTryExample;
public class NestedTryBlockEx4 {
public static void main(String[] args) 
{ 
  String str = "Scientech Easy"; 
  int x[ ] = {0, 1, 2, 3, 4}; 

// Outer try block.
  try 
  { 
    int slength = str.length(); 
    System.out.println("String length: " +slength); 
 // An inner try catch block inside a try block.
    try 
    { 
      int y = 6; 
      System.out.println(x[y]); // Exception occurred. 
    } 
    catch(ArithmeticException ae) // No match is found. 
    { 
       System.out.println("Exception is thrown"); 
       System.out.println(ae.toString()); 
    } 
  } 
  catch(NullPointerException npe) // No match is found. 
  { 
     System.out.println("Exception is thrown "); 
     System.out.println(npe.toString()); 
  } 
  System.out.println("I am out of outer catch block"); 
 } 
}
Output: 
      String length: 14 
      Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 
      at nestedTryExample.NestedTryBlockEx4.main(NestedTryBlockEx4.java:17)

In this example program, the exception thrown by inner try block does not match with the type of outer and inner catch blocks. Therefore, Java runtime system will handle exception at runtime and the program terminates abnormally.

Example 5:

package nestedTryExample;
public class NestedTryBlockEx5 {
public static void main(String[] args) 
{ 
 try 
 { 
// Creating an array of integer values.
   int x[] = {0, 1, 2}; 
   try 
   { 
  // Creating an array inside try block.
     int y[] = {0, 10}; 
     int z = x[2]/y[0]; 
     System.out.println("Division of two numbers: " +z); 
   } 
   catch(ArrayIndexOutOfBoundsException aie) 
   { 
     System.out.println("Inside inner try catch block"); 
     System.out.println(aie.toString()); 
   } 
 } 
 catch(ArithmeticException ae) // No match is found. 
 { 
    System.out.println("Inside outer try catch block "); 
    System.out.println(ae.toString()); 
 } 
 System.out.println("I am out of outer catch block"); 
 } 
}
Output: 
       Inside outer try catch block 
       java.lang.ArithmeticException: / by zero 
       I am out of outer catch block

In this tutorial, we discussed Java nested try-catch blocks with the help of advanced example programs. We hope you have understood how to create a nested try-catch block and practiced all the example programs. Stay tuned our next tutorial in which we will understand return statement in try catch finally block in Java through the example programs.