Chained Exception

Chained exception is a feature introduced with JDK 1.4, which allow developers to associate an exception with another exception.

To explain this further let’s take a simple example. Consider a situation where we try to read content from a file and encounter a NullPointerException. However, the actual problem was an I/O error because of which the file object was not set properly resulting in the NullPointerException later in the code.

Definitely we want the NullPointerException to be thrown because an exception has occurred, we might also want to be aware of the actual cause of the problem. We can do this with chained exception.

Throwable Constructors to Support Chained Exceptions

The Throwable class has two constructors to support chained exception. They allows developers to associate an exception with another exception. The two constructors are shown below:

protected Throwable(String message, Throwable cause)

It creates a new throwable object with the specified detail message and associates it with a cause.

protected Throwable(Throwable cause)

It creates a new throwable object and associates it with a cause.

initCause() and getCause()

Throwable provides two methods that can be used with chained exceptions.

initCause(): This method can be used to initialize the cause of an exception if not otherwise done by the constructor.

getCause(): This method return the cause behind the current exception. If no cause is associated with the current exception it returns null.

Sample Code

Now that we have understood the concept, let’s back it up with an example:


public class DemoChainedException {
    public static void main(String[] args) {
            NullPointerException exception = new NullPointerException();
            exception.initCause(new IOException());
            throw exception;
        }catch(Exception e){    
            System.out.println("Exception Occured: "+e);
            System.out.println("Cause: "+e.getCause());

Below is the output on executing the above class:

Exception Occured: java.lang.NullPointerException

Note: Chained exception can have any depth, which means the cause exception can itself be associated with another cause.


2 ways to Create a Thread in Java

Threads in Java can be created in 2 different ways, extending the Thread class and implementing the Runnable interface. This article explains the two ways with suitable examples.

View Article

4 Different Ways of Creating an Object in Java

Most of us have created object using new operator in Java. Did you know the other ways to create objects in Java. This article talks about 4 different ways of creating objects in Java.

View Article

Using AutoCloseable with 'try-with-resources'

Java 7 added the java.lang.AutoCloseable interface that developers can implement in their custom classes for using them with try-with-resources.

View Article

Shallow Cloning and Deep Cloning

Cloning an object can happen in two ways Deep and Shallow Cloning. In shallow cloning only object references are copied while in deep copy actual objects are copied to the newly cloned object.

View Article

Exception Handling in Inheritance

There are certain rules that must be followed while throwing exceptions in inheritance and overriding methods. This article discuss about these rules.

View Article

Garbage Collection in Java

Java Garbage Collector is a boon to Java developers; it allows developers to program without worrying about memory management. This article describes the working of Java Garbage Collector in details.

View Article

Understanding static and final in Java

Java provides various modifiers which when used with variables, methods or classes, provides them with special properties. Static and final are are two such modifiers which are used frequently by Ja..

View Article

Understanding Object Cloning

Java provides the clone() method which can be used to create a copy an object. It creates a new memory location and copies the content of the object being cloned into the new location.

View Article

Try-with-resources Statement in Java 7

Java 7 introduces the new 'try-with-resources' statement which help developers to overcome the tedious task of closing the resources that have been used in the program.

View Article

Varargs: Variable Arguments to Methods

Java 5 introduced a new feature called varargs, which allows methods to accept variable number of arguments. They are useful in cases where the number of arguments to be passed to a method is unknown.

View Article