Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Nested Try in Java | Nested Try Catch Java

Nested Try in Java | In the previous tutorial so far, we learned a single try-Catch block, multiple catch blocks in a java program.

Now we will learn nesting of multiple try-catch blocks inside one try block in a program. Java 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:
Syntax:

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

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.

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 Programs


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

Program source 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]); 
} 
catch(ArrayIndexOutOfBoundsException aie) 
{
 System.out.println("Exception is thrown"); 
 System.out.println(aie.toString()); 
 } 
} 
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, an exception occurred inside the outer catch block. So, the control of execution passes to the outer catch block to handle exception without execution of the inner catch block.

Program source 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()); 
 } 
} 
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, 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 source 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 catch 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.

So, the control is passed to the outer catch block to handle the exception thrown by inner block.

Program source code 4:

package nestedTryExample;
public class NestedTryBlockEx4 
{
public static void main(String[] args) 
{ 
 String str = "Scientech Easy"; 
 int x[ ] = {0, 1, 2, 3, 4}; 
try 
{ 
 int slength = str.length(); 
 System.out.println("String length: " +slength); 

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 the above program, the exception thrown by inner catch 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 source code 5:

public class NestedTryBlockEx5 
{
public static void main(String[] args) 
{ 
try 
{ 
 int x[] = {0, 1, 2}; 
try 
{ 
 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

Hope that this tutorial has covered almost all the important points related to Java nested try catch block with example programs. I hope that you will have understood example programs.
Thanks for reading!!!
Next ⇒ Finally Block in Java⇐ PrevNext ⇒

The post Nested Try in Java | Nested Try Catch Java appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Nested Try in Java | Nested Try Catch Java

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×