Javatpoint Logo
Javatpoint Logo

Chained Exceptions in Java

In Java, a chained exception is a technique that enables programmers to associate one Exception with another. By providing additional information about a specific exception, debugging can be made easier. A chained exception is created by wrapping an existing exception in a new exception, which becomes the root cause of the new Exception.

The new Exception can provide additional information, while the original Exception contains the actual error message and stack trace. It makes it easier to determine and fix the problem's source. Chained exceptions are especially useful when an exception is thrown due to another exception.

In Java, a chained exception is created using one of the constructors of the exception class.

Throwable Class

Constructors and methods for supporting chained exceptions are available in the Throwable class. Let's start by taking a look at the constructors.

Throwable(Throwable cause): A Java constructor creates a new exception object with a specified cause exception.

Throwable(String desc, Throwable cause): A Java constructor creates a new Throwable object with a message and a cause. It allows for chaining exceptions and providing more detailed information about errors.

In Java, the following Throwable class methods enable chained exceptions:

getCause(): It is a Java method of the Throwable class that returns the cause of the current Exception. It allows for accessing the Exception or error that triggered the current Exception to be thrown.

initCause() method: determines the reason for the calling Exception.

Example of Chained Exception:



Exception in thread "main" java.lang.RuntimeException: Exception at ChainedExceptionExample.main(
Caused by: Java.lang.NullPointerException: It is actual cause of the exception at ChainedExceptionExample.main(

Example of with and without chained Exception

Without Chained Exception:



Enter a positive integer: -9
Invalid input: Number must be positive

With Chained Exception:



Enter a positive integer: -9
Exception in thread "main" java.lang.RuntimeException: Invalid input at ChainedExceptionExample.main(
Caused by: Java.lang.IllegalArgumentException: Number must be positive at ChainedExceptionExample.main(

Difference Between non chained and chained Exception

Non-chained exceptions Chained exceptions
An exception occurs and is caught at the point where it occurs without any reference to the original cause of the Exception. The original cause of the Exception is preserved and reported along with the current Exception.
A lack of reference to the original cause can make debugging and troubleshooting more challenging. Chained exceptions provide more context to the Exception and make it easier to identify and fix the root cause of the problem.
Suitable for small programs or simple systems where exceptions are limited and easy to identify Particularly useful in large programs or complex systems where exceptions can be thrown across different components, as it can help identify where the problem first occurred and how it propagated through the system

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA