The initCause() method of the Throwable class is used to associate the original cause of an exception with another exception. It helps maintain the complete exception chain, making it easier to identify the root cause of an error during debugging.
- Helps create an exception chain for better debugging.
- Can be called only once on a Throwable object.
- Returns the current Throwable object after setting the cause.
public class Main {
public static void main(String[] args) {
try {
Exception original = new Exception("File not found");
Exception wrapper = new Exception("Unable to load file");
wrapper.initCause(original);
throw wrapper;
}
catch (Exception e) {
System.out.println("Message: " + e.getMessage());
System.out.println("Cause: " + e.getCause());
}
}
}
Output
Message: Unable to load file Cause: java.lang.Exception: File not found
Explanation: In this example, an exception with the message "File not found" is created as the original cause. Another exception "Unable to load file" is then created, and initCause() links the original exception as its cause. When the wrapper exception is caught, getCause() returns the original exception, helping identify the actual reason for the error.
Syntax
public Throwable initCause(Throwable cause)
- Parameter:
cause:The exception that caused the current exception. - Returns: A reference to the current
Throwableobject.
import java.io.*;
class GFG {
// Main Method
public static void main(String[] args)
throws Exception
{
try {
testException1();
}
catch (Throwable e) {
System.out.println("Cause : "
+ e.getCause());
}
}
// method which throws Exception
public static void testException1()
throws Exception
{
// ArrayIndexOutOfBoundsException
// This exception will be used as a Cause
// of another exception
ArrayIndexOutOfBoundsException
ae
= new ArrayIndexOutOfBoundsException();
// create a new Exception
Exception ioe = new Exception();
// initialize the cause and throw Exception
ioe.initCause(ae);
throw ioe;
}
}
Output
Cause : java.lang.ArrayIndexOutOfBoundsException
Explanation: In this example, an ArrayIndexOutOfBoundsException is created and set as the cause of another Exception using the initCause() method. When the new exception is thrown and caught, getCause() returns the original ArrayIndexOutOfBoundsException.
Example: Java program to demonstrate the initCause() Method.
import java.io.*;
class GFG {
// Main Method
public static void main(String[] args)
throws Exception
{
try {
// add the numbers
addPositiveNumbers(2, -1);
}
catch (Throwable e) {
System.out.println("Cause : "
+ e.getCause());
}
}
// method which adds two positive number
public static void addPositiveNumbers(int a, int b)
throws Exception
{
// if Numbers are Positive
// than add or throw Exception
if (a < 0 || b < 0) {
// create a Exception
// when Numbers are not Positive
// This exception will be used as a Cause
// of another exception
Exception
ee
= new Exception("Numbers are not Positive");
// create a new Exception
Exception anotherEXe = new Exception();
// initialize the cause and throw Exception
anotherEXe.initCause(ee);
throw anotherEXe;
}
else {
System.out.println(a + b);
}
}
}
Output
Cause : java.lang.Exception: Numbers are not Positive
Explanation: In this example, if either number is negative, an exception with the message "Numbers are not Positive" is created. This exception is then set as the cause of another exception using initCause(). When the exception is caught, getCause() displays the original exception and its message, helping identify the actual reason for the error.
References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable)