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?

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?

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 .