Java Throwable getCause() method

It is used to fetch the cause of the Throwable or null if cause can't be determined. This function fetches the cause that was supplied by one of the constructors or that was set after creation with the initCause(Throwable) method. All the PrintStackTrace methods invoke getCause() method to determine the cause of the Throwable.

Syntax

Return

It returns the cause of the Throwable or null if cause can't be determined.

Example 1

Output:

Cause : java.lang.ArithmeticException: / by zero

Example 2

Output:

java.lang.ArithmeticException: / by zero
	at ExceptionTest.main(ExceptionTest.java:5)
Cause : null