Throwable getCause() method in Java with Examples
The getCause() method of Throwable class is the inbuilt method used to return the cause of this throwable or null if cause can’t be determined for the Exception occurred. This method help to get 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 of Throwable class invoke getCause() method to determine the cause of the Throwable or Exception. In simple terms, it can be said that this method returns the cause because of which Exception occurred.
Syntax:
public Throwable getCause()
Return Value: This method returns the cause of this Throwable or null if cause can’t be determined.
Below programs demonstrate the getCause() method of Throwable Class:
Example 1:
// Java program to demonstrate // the ensureCapacity() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // divide the numbers divide(2, 0); } catch (ArithmeticException e) { System.out.println("Cause of Exception: " + e.getCause()); } } // method which divides two number public static void divide(int a, int b) throws Exception { try { // divide two numbers int i = a / b; } catch (ArithmeticException e) { // initializing new Exception with cause ArithmeticException exe = new ArithmeticException(); exe.initCause(e); throw(exe); } } } |
Cause of Exception: java.lang.ArithmeticException: / by zero
Example 2:
// Java program to demonstrate // the ensureCapacity() Method. import java.io.*; class GFG { // Main Method public static void main(String[] args) throws Exception { try { // divide the numbers divide(2, 0); } catch (ArithmeticException e) { System.out.println("Cause of Exception : " + e.getCause()); } } // method which divides two number public static void divide(int a, int b) throws Exception { // divide two numbers int i = a / b; } } |
Cause of Exception : null
References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getCause()
Recommended Posts:
- Throwable getSuppressed() method in Java with Examples
- Throwable getLocalizedMessage() method in Java with Examples
- Throwable initCause() method in Java with Examples
- Throwable toString() method in Java with Examples
- Throwable addSuppressed() method in Java with Examples
- Throwable setStackTrace() method in Java with Examples
- Throwable getMessage() method in Java with Examples
- Throwable getStackTrace() method in Java with Examples
- Throwable printStackTrace() method in Java with Examples
- Throwable fillInStackTrace() method in Java
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java lang.Long.reverse() method in Java with Examples
- Java lang.Long.byteValue() method in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



