Java Chained Exceptions

« Previous Tutorial Next Tutorial »

With chained exceptions feature, you can associate another exception with an exception. The second exception accounts the cause of first exception. For instance, imagine a situation in which a method throws an ArithmeticException because of an attempt to divide-by-zero. Nevertheless, the actual cause of problem was that an I/O error occurred, which caused the divisor to be set improperly.

Even though the method must certainly throw an ArithmeticException, since that is the error that occurred, you might want to let the calling code know that the underlying cause was an I/O error. Chained exceptions let you to manage this, and any other situation in which layers of exceptions exist.

How to allow Chained Exceptions in Java ?

To allow chained exceptions in Java, two constructors and two methods were added to the Throwable. The constructors are shown here:

Throwable(Throwable causeExc)
Throwable(String msg, Throwable causeExc)

In the first form, causeExc is the exception, causes the current exception i.e., causeExc is underlying reason that an exception occurred. And the second form allows you to specify a description at the same time that you specify a cause exception. These two constructors have been added to the Error , Exception, and RuntimeException classes also.

The methods getCause() and initCause() are the chained exception methods that supported by Throwable. These methods are shown in table (in the previous chapter) and are repeated here for the sake of discussion.

Throwable getCause()
Throwable initCause(Throwable causeExc)

The method getCause() returns the exception that underlies the current exception. If there is no underlying exception, null is returned.

The method initCause() associates causeExc with the invoking exception and returns a reference to the exception. Therefore, you can associate a cause with an exception after the exception has been created. Nevertheless, the cause exception can be set only once. Therefore, you can call the initCause() only once for each exception object. Moreover, if the cause exception was set by a constructor, then you can not set it again using the initCause().

In general, the initCause() is utilized to set a cause for legacy exception classes that don't support the two additional constructors described earlier.

Java Chained Exception Example

Below is an example program that demonstrates the mechanics of handling the chained exceptions

/* Java Program Example - Java Chained Exceptions
*  This program demonstrates the exception chaining   */
 class JavaProgram
     static void demometh()
         /* creating an exception */
         NullPointerException e = new NullPointerException("top layer");
         /* add a cause */
         e.initCause(new ArithmeticException("cause"));
         throw e;
     public static void main(String args[])
		 catch(NullPointerException e)
             /* display the top level exception */
             System.out.println("caught : " + e);
             /* display the cause exception */
             System.out.println("original cause : " + e.getCause());

When the above Java program is compile and executed, it will produce the following output:

java chained exceptions

In this example, NullPointerException is the top-level exception. To it is added a cause exception, ArithmeticException. When the exception is thrown out of demometh() method, it is caught by the main(). There, the top-level exception is displayed, followed by underlying exception, which is obtained by calling the method getCause().

Chained exceptions can be carried on to whatever depth is necessary. Therefore, the cause exception can, itself, have a cause. Be aware that overly long chains of exceptions may show poor design.

Chained exceptions are not something that every program will need. However, in cases in which knowledge of an underlying cause is useful, then they offer an elegant solution.

« Previous Tutorial Next Tutorial »

Latest 5 Posts by CodesCracker


Quick Links
Signup - Login - Give Online Test