![]() |
VOOZH | about |
The getCause() method of the Throwable class is used to retrieve the original cause of an exception. If no cause is associated with the exception, it returns null.
Example 1: Exception Chaining with initCause()
Cause of Exception: java.lang.ArithmeticException: / by zero
Explanation:
public Throwable getCause()
Return Value: Returns the Throwable that caused this exception, or null if the cause is unknown.Class:
Example 2: Without Exception Chaining
Cause of Exception : null
Explanation: Here, no cause was explicitly set, so getCause() returns null.
Key Points:
- getCause() helps debug chained exceptions by identifying the root cause.
- Use initCause(Throwable cause) to link exceptions manually.
- Always check for null to avoid NullPointerException.