In Java, the Throwable
class is the superclass of all error and exception classes. The initCause()
method of the java.lang.Throwable
class is used to initialize the cause of a throwable exception to the specified value.
The declaration for the initCause()
method of the java.lang.Throwable
class is as follows:
public Throwable initCause(Throwable cause) throws IllegalStateException
cause
: This is the cause of the Throwable
exception, which is saved for later retrieval by the getCause()
method. A null value is permitted, and indicates that the cause is nonexistent or unknown.
Throwable
: The method returns a reference to this Throwable
instance.
IllegalArgumentException
: We get this exception if the cause is this throwable. A throwable cannot be its own cause.
IllegalStateException
: This method is intended to be used only once. If it is called more than once, then it will throw an IllegalStateException
.
Consider a situation where a client sends a request to a server and the request is unsuccessful. The server may respond with an HTTP status code that indicates that the request was unsuccessful. The initCause()
method can be used to initialize the cause of the HTTP status code.
Let's look at a code example to see how this can be achieved.
public class InitCauseExample {public static void main(String args[] ) {try {String test = null;test.length();} catch (NullPointerException ex) {// get the cause and print itThrowable t = ex.getCause();System.out.println("Cause is: " + ex.getCause() );// initialize the cuase of the NullPointerException to nullex.initCause(null);// now, try to initialize the cause again and observe the resultex.initCause(new ArithmeticException("This is the cause"));}}}
This will produce the following output.
Cause is: nullException in thread "main" java.lang.IllegalStateException: Can't overwrite cause with java.lang.NullPointerException: This is the causeat java.lang.Throwable.initCause(Throwable.java:458)at InitCauseExample.main(InitCauseExample.java:16)Caused by: java.lang.NullPointerExceptionat InitCauseExample.main(InitCauseExample.java:5)
In the example above, the initCause()
method is called twice. The first time it is called, it initializes the cause of the NullPointerException
to null
. The second time it is called, it throws an IllegalStateException
because the cause has already been initialized.
public class InitCauseExample {public static void main(String args[] ) {try {String test = null;test.length();} catch (NullPointerException ex) {// get the cause and print itThrowable t = ex.getCause();System.out.println("Cause is: " + ex.getCause() );// initialize the cause to itselfex.initCause(ex);}}}
In the code above, the cause of the NullPointerException
is initialized to itself using the initCause()
method. This will throw an IllegalArgumentException
because a throwable cannot be its own cause.
The output of the above code is as follows.
Exception in thread "main" java.lang.IllegalArgumentException: Self-causation not permittedat java.lang.Throwable.initCause(Throwable.java:460)at InitCauseExample.main(InitCauseExample.java:13)Caused by: java.lang.NullPointerExceptionat InitCauseExample.main(InitCauseExample.java:5)
In this answer, we saw the initCause()
method of the Throwable
class. The initCause()
method is used to initialize the cause of a throwable to the specified value. This method should be used only once. If it is called more than once, it will throw an IllegalStateException
. You cannot initialize the cause of a throwable to itself, as it will throw an IllegalArgumentException
.