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

Hot Searches
struts-config.xml  web.xml  Java date format  read/write text file using javascript  ArrayList  Insert Data into Database Using Hibernat  create text file using java  insert data to text file using java  Visual Basic Date and Time Display the t  Create table and insert data by sql quer 

  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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 .

                         

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 
Join and Excel yourself with our Online instructor led training sessions
Integrated Struts 2 Hibernate and JPA Training
Java Training
Hibernate Training
JPA Training
JSF Training
Spring Framework Training
Struts 2 Training
EJB 3 Training
PHP Training
Junit Training
DbUnit Training
Ajax Training
IBM Websphere Training
Design Patterns Training
Weblogic Training
Tomcat Training
JMS Training
Jboss Training
Servlet Training
JSP Training
JavaFX Training
SQL Server Training
TPTP Training
Tell A Friend
Your Friend Name

 

 
 

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