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
java_lang_throwable.htm
Advertisements