Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
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.

 

 

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:

                         

» View all related tutorials
Related Tags: c exception orm event io struct exec uri int exe condition call interrupt program execution ram ical flow pic exceptions

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.