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

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
What are Chained Exceptions? 
 

Whenever in a program the first exception causes an another exception, that is termed as Chained Exception. Java provides new functionality for chaining exceptions.

 

What are Chained Exceptions in Java?

                         

Whenever in a program the first exception causes an another exception, that is termed as Chained Exception. Java provides new functionality for chaining exceptions. Exception chaining (also known as "nesting exception") is a technique for handling the exception, which occur one after another i.e. most of the time is given by an application to response to an exception by throwing another exception. Typically the second exception is caused by the first exception. Therefore chained exceptions help the programmer to know when one exception causes another. 

The constructors that support chained exceptions in Throwable class are:

Throwable initCause(Throwable)
Throwable(Throwable)
Throwable(String, Throwable)
Throwable getCause()

The methods of the Throwable class are:

METHOD DESCRIPTION
toString() Returns the exception followed by a message string (if one exit) .
getMessage() Returns the message string of the Throwable object.
printStackTrace() Returns the full name of the exception class and some additional information apart from the information of first two method.
getCause() Returns the exception that caused the occurrence of current exception.
initCause() Returns the current exception due to the Throwable constructors and the Throwable argument to initCause.

The syntax for using a chained exception is as follows in which a new TestException exception is created with the attached cause when an IOException is caught. Thus the chain exception is thrown to next level of exception handler.

try {
} catch (IOException e) {
throw new TestException("Other IOException", e);
}

Lets  see an example having the implementation of  chained exceptions:

import java.io.*;
import java.util.*;
class MyException extends Exception{
MyException(String msg){
         super(msg);
   }
}
public class ChainExcep{
              public static void main(String args[])throws MyException, IOException{
              try{
           int rs=10/0;
               }
catch(Exception e){
            System.err.println(e.getMessage());
         System.err.println(e.getCause());
        throw new MyException("Chained ArithmeticException");
           }
          }
  }

Output of the Program:

C:\Roseindia\>javac ChainExcep.java

C:\Roseindia\>java javac ChainExcep

/ by zero
null
Exception in thread "main" MyException: Chained ArithmeticException
at ChainExcep.main(ChainExcep.java:21)

This example has an user defined exception that throws an ArithmeticException and has been thrown under the catch handler. After throwing an exception, the handler will execute the statement of the catch block and then invoke the user defined constructor. Thus the implementation of chained exception is very helpful to the user to make a program or an application error and exception free.

Download this example 

Now lets see the two different ways to generate an Exception.

  1. Exceptions generated by the Java run-time system - These are the exceptions which violate the rules of the Java language and are related to some fundamental errors.
  2. Manually generated Exceptions - These are the exceptions which are being generated manually by your code and by which some of the error conditions are reported to the caller of a method .

                         

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