Chained Exceptions in Java with Example

Java 2, version 1.4 added a new feature chained exceptions. The chained exceptions feature relates one exception with another exception. The second exception explains the cause of the first exception.

Chained exception in Java is a technique to handle exceptions that occur one after another. This technique helps us to know when one exception causes another.

For example, let us assume that a, b, and c are objects of three different exception types A, B, and C respectively. The object a of type A causes an exception of type B to occur and an object of B type also causes an exception of C type.

This process is called chaining of exceptions and the exceptions involved in this process are called chained exceptions in Java.

Constructors added to Throwable class to support Chained Exceptions

To implement chained exceptions feature in JDK 1.4 version, Java added two constructors and two methods to Throwable class. The constructors that support chained exception in java are as follows:

1. Throwable(Throwable causeExc) // causeExc is exception that causes current exception.
2. Throwable(String msg, causeExc) // msg is an exception message and causeExc is exception that causes the current exception.

Methods added to Throwable class to support Chained Exceptions

There are the following methods added to the Throwable class that supports chained exceptions.

1. toString(): This method returns an exception followed by a description of the exception. The general syntax is as follows:

public String toString()

2. getMessage(): It returns the description of exception. The general syntax is as:

public String getMessage()

3. printStackTrace(): This method displays stack trace. It returns nothing. The general syntax is given below.

public void printStackTrace()

4. getClause(): The getClause() method returns the exception that caused the occurrence of current exception. If there is no caused exception then null is returned. The syntax is as follows:

public Throwable getClause()

5. initCause(): The initCause() method joins “causeExc” with the invoking exception and returns a reference to the exception.

public Throwable initCause(Throwable causeExc)

Java Chained Exceptions Example Programs

Let’s take an example program based on java chained exceptions. In this example program, we are making ArithmeticException caused by StringIndexOutOfBoundsException, and StringIndexOutOfBoundsException caused by ArrayStoreException.

Let’s see the source code to understand better.

Program source code 1:

public class ChainedException 
static void chained()
 ArithmeticException a = new ArithmeticException();
 StringIndexOutOfBoundsException b = new StringIndexOutOfBoundsException();
 ArrayStoreException c = new ArrayStoreException();
Throwable t1 = b.initCause(a); // t1 is an object of type Throwable class. The initCause() method returns reference to Throwable class type and stored in a variable t1. 
Throwable t2 = c.initCause(b); // t2 is an object of type Throwable class.
System.out.println(b.getCause()+ " caused \n" +b);
System.out.println(c.getCause() + " caused \n" +c);
public static void main(String[] args) 
      java.lang.ArithmeticException caused 
      java.lang.StringIndexOutOfBoundsException caused 

As you can observe in the above program, an ArithmeticException is raised by StringIndexOutOfBoundsException. If the exception is not caused by another exception, the intiCause() method returns null.

Program source code 2:

public class OwnException extends Exception
 OwnException(String str)
public class ChainedExcep 
public static void main(String[] args) throws OwnException 
try {
   int x = 10/0;
   System.out.println("Result: " +x);
catch(ArithmeticException e)
   throw new OwnException("Chained Exception");
       / by zero
       Exception in thread "main" chainedExceptions.OwnException: Chained Exception
	at chainedExceptions.ChainedExcep.main(

In the preceding example program, user-defined exception throws ArithmeticException under catch block. After throwing an exception, JVM will execute statements of catch block, and then calls user-defined constructor.

Since the exception is not caused by another exception, the getCause() method returns null.

Hope that this tutorial has covered all important points related to chained exceptions in Java with example programs. I hope that you will have understood and enjoyed this topic.
Thanks for reading!!!
Next ⇒ Throwable class in Java

⇐ Prev Next ⇒

Please share your love