What is the initCause() method in Java?

Overview

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.

Syntax

The declaration for the initCause() method of the java.lang.Throwable class is as follows:

public Throwable initCause(Throwable cause) throws IllegalStateException
Declaration of the java.lang.Throwable.initCause() method

Parameters

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.

Return value

Throwable: The method returns a reference to this Throwable instance.

Exception

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.

Code example

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 it
Throwable t = ex.getCause();
System.out.println("Cause is: " + ex.getCause() );
// initialize the cuase of the NullPointerException to null
ex.initCause(null);
// now, try to initialize the cause again and observe the result
ex.initCause(new ArithmeticException("This is the cause"));
}
}
}

This will produce the following output.

Cause is: null
Exception in thread "main" java.lang.IllegalStateException: Can't overwrite cause with java.lang.NullPointerException: This is the cause
at java.lang.Throwable.initCause(Throwable.java:458)
at InitCauseExample.main(InitCauseExample.java:16)
Caused by: java.lang.NullPointerException
at InitCauseExample.main(InitCauseExample.java:5)
Output of the above code

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 it
Throwable t = ex.getCause();
System.out.println("Cause is: " + ex.getCause() );
// initialize the cause to itself
ex.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 permitted
at java.lang.Throwable.initCause(Throwable.java:460)
at InitCauseExample.main(InitCauseExample.java:13)
Caused by: java.lang.NullPointerException
at InitCauseExample.main(InitCauseExample.java:5)

Conclusion

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.

Free Resources