Home Java Exceptions Nested Try-Catch Blocks
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Nested Try-Catch Blocks

Advertisement
In Java we can have nested try and catch blocks. It means that, a try statement can be inside the block of another try.

Nested Try-Catch Blocks

     

In Java we can have nested try and catch blocks. It means that, a try statement can be inside the block of another try. If an inner try statement does not have a matching catch statement for a particular exception, the control  is transferred to  the next try statement?s catch handlers that are expected for a matching catch statement. This continues until one of the catch statements succeeds, or until all of the nested try statements are done in.  If no one catch statements match, then the Java run-time system will handle the exception. 

The syntax of nested try-catch blocks is given below: 

try {
  try {
  // ...
  } 

  catch (Exception1 e) 

  {
   //statements to handle the exception
  }
   } 

  catch (Exception 2 e2)

  {
   //statements to handle the exception 
  }

 

Lets have an example that uses the nested try-catch blocks

import java.io.*;
public class NestedTry{
  public static void main (String args[])throws IOException  {
  int num=2,res=0;
  
  try{
  FileInputStream fis=null;
  fis = new FileInputStream (new File (args[0]));
  try{
  res=num/0;
  System.out.println("The result is"+res);
  }
  catch(ArithmeticException e){
  System.out.println("divided by Zero");
  }
  }
  catch (FileNotFoundException e){
  System.out.println("File not found!");
  }
  catch(ArrayIndexOutOfBoundsException e){
  System.out.println("Array index is Out of bound! Argument required");
  }
  catch(Exception e){
  System.out.println("Error.."+e);
  }
}
}

Output of the program:

C:\Roseindia\>javac NestedTry.java

C:\Roseindia\>java NestedTry

Array index is Out of bound! Argument required

In this given example we have implemented nested try-catch blocks concept where an inner try block is kept with in an outer try block, that's catch handler will handle the arithmetic exception. But before that an  ArrayIndexOutOfBoundsException will be raised, if a file name is not passed as an argument while running the program. Now lets see, what will be the output if we pass the file name student as an argument.

C:\Roseindia\>javac NestedTry.java

C:\Roseindia\>java NestedTry student.txt

File not found!

If the outer try block's statements run successfully then the inner try will raise an ArithmeticException as:

C:\Roseindia\>javac NestedTry.java

C:\Roseindia\>java NestedTry student.txt

divided by Zero

Download this example:

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Ask Questions?    Discuss: Nested Try-Catch Blocks   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com