Once an application finds an exception, responds an exception by throwing another exception. It causes another exception. Knowing an exception that causes another exception is very useful.
Chained exceptions helps in finding the root cause of the exception that occurs during application’s execution. The methods that support chained exceptions are getCause(), initCause() and the constructors Throwable(Throwable), Throwable(String, Throwable). The reason / cause for the current exception is returned by the method getCause(). With initCause() method, the current exception’s cause is set.
The following example illustrates the use of chained exception.
try {
} catch (IOException e) {
throw new OtherException("Other IOException", e);.
}
When the IOException is caught, a new OtherException is created with the original cause and the chain of exceptions is thrown up to the exception handler at higher level.
Chained exceptions helps in finding the root cause of the exception that occurs during application’s execution. The methods that support chained exceptions are getCause(), initCause() and the constructors Throwable(Throwable), Throwable(String, Throwable). The reason / cause for the current exception is returned by the method getCause(). With initCause() method, the current exception’s cause is set.
The following example illustrates the use of chained exception.
try {
} catch (IOException e) {
throw new OtherException("Other IOException", e);.
}
When the IOException is caught, a new OtherException is created with the original cause and the chain of exceptions is thrown up to the exception handler at higher level.