An application often responds to an exception by throwing another exception. In effect, the first exceptioncausesthe second exception. It can be very helpful to know when one exception causes another. Chained Exceptions help the programmer do this.
The following are the methods and constructors in Throwable that support chained exceptions.
The Throwable argument to initCause and the Throwable constructors is the exception that caused the current exception. getCause returns the exception that caused the current exception, and initCause sets the current exception's cause.
The following example shows how to use a chained exception.
In this example, when an IOException is caught, a new SampleException exception is created with the original cause attached and the chain of exceptions is thrown up to the next higher level exception handler.
Lets see an example to handle chained exception using multiple catch blocks.
Arithmetic exception occurred :
java.lang.ArithmeticException: / by zero
Chained exception thrown manually :