How do I chain exceptions?
Author: Deron Eriksson
Description: This tutorial describes how to chain exceptions in Java.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0)


Page:    1 2 >

Sometimes, when you catch an exception, you want to throw a new exception. However, you may want to convey the information in the original exception in the new exception. This can be done by chaining exceptions.

First off, let's look at a code snippet that catches an exception and then throws a new exception without chaining. (Note: in these examples, I write the stacktraces to standard output rather than standard error.)

...
	System.out.println("***no chaining example:");
	try {
		try {
			throw new Exception("One");
		} catch (Exception e) {
			throw new Exception("Two");
		}
	} catch (Exception e) {
		e.printStackTrace(System.out);
	}
...

The output from the above code is shown below. Notice that the information from the inner exception is lost when we display the stacktrace of the outer exception.

***no chaining example:
java.lang.Exception: Two
	at test.ExceptionTest.main(ExceptionTest.java:11)

Now, let's look at an example of chaining exceptions. In the inner try/catch block, we throw an Exception object with the message "Three". In its catch block, we catch the Exception and then throw a new Exception. However, notice the constructor of this Exception. It takes a message String ("Four") and then a Throwable (e). The Throwable parameter allows us to chain the original exception to the new exception.

...
		System.out.println("\n***chaining example 1:");
		try {
			try {
				throw new Exception("Three");
			} catch (Exception e) {
				throw new Exception("Four", e);
			}
		} catch (Exception e) {
			e.printStackTrace(System.out);
			System.out.println("###what was the cause:");
			e.getCause().printStackTrace(System.out);
		}
...

In the outer try/catch block, we catch the exception and display its stacktrace. Notice that the stacktrace includes the new message ("Four") along with the original message ("Three"). In addition, as a bonus, I called e.getCause() on the "Four" exception to get the exception that's the cause for the "Four" exception, which is the "Three" exception. Thus, we see that we can actually get the original exception from the new exception.

***chaining example 1:
java.lang.Exception: Four
	at test.ExceptionTest.main(ExceptionTest.java:22)
Caused by: java.lang.Exception: Three
	at test.ExceptionTest.main(ExceptionTest.java:20)
###what was the cause:
java.lang.Exception: Three
	at test.ExceptionTest.main(ExceptionTest.java:20)

As another short example of chaining, here we throw an Exception in the inner try/catch block, and we catch the Exception and throw a new Exception. However, notice that this time, when we throw the new Exception, we use a constructor that takes a single argument, a Throwable, so we use our caught Exception e as the parameter to the constructor.

...
		System.out.println("\n***chaining example 2:");
		try {
			try {
				throw new Exception("Five");
			} catch (Exception e) {
				throw new Exception(e);
			}
		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
...

In the outer try/catch block, we catch this exception and display the stacktrace, shown below. Notice that it displays the chained exceptions.

***chaining example 2:
java.lang.Exception: java.lang.Exception: Five
	at test.ExceptionTest.main(ExceptionTest.java:35)
Caused by: java.lang.Exception: Five
	at test.ExceptionTest.main(ExceptionTest.java:33)

(Continued on page 2)

Page:    1 2 >