
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java Throwable getCause() Method
Description
The Java Throwable getCause() method returns the cause of this throwable or null if the cause is nonexistent or unknown
Declaration
Following is the declaration for java.lang.Throwable.getCause() method
public Throwable getCause()
Parameters
NA
Return Value
This method returns the cause of this throwable or null if the cause is nonexistent or unknown.
Exception
NA
Example: Getting Initial Cause of Throwable
The following example shows the usage of java.lang.Throwable.getCause() method. In this program, we've defined a method raiseException() where we've defined a Throwable and using initCause() method, we've set a initial cause as a throwable with message ABCD. In main method, raiseException() method is called. In catch block, we handles the exception and print the cause using getCause() method.
package com.tutorialspoint; public class ThrowableDemo { public static void main(String[] args) throws Throwable { try { raiseException(); } catch(Throwable e) { System.out.println(e.getCause()); } } public static void raiseException() throws Throwable { Throwable t = new Throwable("This is new Exception..."); t.initCause(new Throwable("ABCD")); throw t; } }
Output
Let us compile and run the above program, this will produce the following result −
java.lang.Throwable: ABCD
Example: Getting Initial Cause of RuntimeException
The following example shows the usage of java.lang.Throwable.getCause() method. In this program, we've defined a method raiseException() where we've defined a RuntimeException and using initCause() method, we've set a initial cause as a throwable with message ABCD. In main method, raiseException() method is called. In catch block, we handles the exception and print the cause using getCause() method.
package com.tutorialspoint; public class ThrowableDemo { public static void main(String[] args) throws Throwable { try { raiseException(); } catch(Exception e) { System.out.println(e.getCause()); } } public static void raiseException() throws RuntimeException { RuntimeException t = new RuntimeException("This is new Exception..."); t.initCause(new Throwable("ABCD")); throw t; } }
Output
Let us compile and run the above program, this will produce the following result −
java.lang.Throwable: ABCD
Example: Getting Initial Cause of Exception
The following example shows the usage of java.lang.Throwable.getCause() method. In this program, we've defined a method raiseException() where we've defined a Exception and using initCause() method, we've set a initial cause as a throwable with message ABCD. In main method, raiseException() method is called. In catch block, we handles the exception and print the cause using getCause() method.
package com.tutorialspoint; public class ThrowableDemo { public static void main(String[] args) throws Throwable { try { raiseException(); } catch(Exception e) { System.out.println(e.getCause()); } } public static void raiseException() throws Exception { Exception t = new Exception("This is new Exception..."); t.initCause(new Throwable("ABCD")); throw t; } }
Output
Let us compile and run the above program, this will produce the following result −
java.lang.Throwable: ABCD