Nested Try in Java | Nested Try Catch Java

Nested Try in Java | In the previous tutorial, so far, we have known about the execution process of single try-catch block, and multiple catch block in Java with the help of various examples.

Now we will learn nesting of multiple try-catch blocks inside one try block in a program. Java also supports nested try block just like multiple catch blocks.

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.

Syntax of Java Nested Try Block


The syntax of 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.

Let’s understand the different cases associated with them.


Case 1:

If an exception occurs within 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 outer try block, the control of execution enters into the inner try block. If an exception occurs inside 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.

Program code 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.

Program code 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.

Program code 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.

Program code 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.

Program code 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 have discussed Java nested try catch block with the help of some advanced example programs. Hope that you will have understood how to create a nested try catch block in Java and practiced all examples.

In the next tutorial, we will learn about finally block in Java with the help of some important advanced example programs.
Thanks for reading!!!

⇐ PrevNext ⇒

Please share your love