What is chained exceptions in java?

What is chained exceptions in java?

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.
When should we create our own custom exception classes? - Java
The term exception lets the programmer to know that there is something exceptional has occurred in the program. Apart from Java exception class library, a custom exception can be created by the developer...
What are primitive type wrappers classes? - Java
Primitive type wrapper classes or simply wrapper classes are available in java.lang package for providing object methods for all the eight primitive types. All the wrapper class objects are immutable...
Why do you recommend that the main thread be the last to finish? - Java
In an application, a program continues to run until all of the threads have ended. Hence, the main thread to finish at last is not a requirement. It is a good programming practice to make it to run last to finish...
Post your comment