Java Throwable initCause() Method



Description

The Java Throwable initCause() method initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.) It is generally called from within the constructor, or immediately after creating the throwable

Declaration

Following is the declaration for java.lang.Throwable.initCause() method

public Throwable initCause(Throwable cause)

Parameters

cause − This is the cause (which is saved for later retrieval by the getCause() method). (A null value is permitted, and indicates that the cause is nonexistent or unknown.)

Return Value

This method returns a reference to this Throwable instance.

Exception

  • IllegalArgumentException − if cause is this throwable.

  • IllegalStateException − if this throwable was created with Throwable(Throwable) or Throwable(String,Throwable), or this method has already been called on this throwable.

Example: Getting Initial Cause of Throwable

The following example shows the usage of java.lang.Throwable.initCause() method. In this program, we've defined two Classes, CustomException and OtherException to represent Exception classes along with two methods raiseException() and raiseAnotherException(). raiseException() calls raiseAnotherException() which in order throws OtherException as root exception. In catch block of raiseException method, we've used initCause() method to set the initial cause of the exception raised.In main method, raiseException() method is called. In catch block, we handles the exception and print it which shows the initial cause.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         System.out.println(e);
      }
   }
  
   public static void raiseException()throws CustomException{
      try {
         raiseAnotherException();
      } catch(OtherException e) {
         CustomException a1 = new CustomException();
     
         // initializes the cause of this throwable to the specified value. 
         a1.initCause(e);
         throw a1;
      }
   }
  
   public static void raiseAnotherException() throws OtherException {
      throw new OtherException();
   }
}

class CustomException extends Throwable {
   CustomException() {
      super("This is my Exception....");
   }
}

class OtherException extends Throwable {
   OtherException() {
      super("This is any other Exception....");
   }
} 

Output

Let us compile and run the above program, this will produce the following result −

Exception in thread "main" com.tutorialspoint.CustomException: This is my Exception....
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:17)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: com.tutorialspoint.OtherException: This is any other Exception....
	at com.tutorialspoint.ThrowableDemo.raiseAnotherException(ThrowableDemo.java:26)
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	... 1 more

Example: Getting Initial Cause of RuntimeException

The following example shows the usage of java.lang.Throwable.initCause() 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 it which shows the initial cause.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
  
   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.RuntimeException: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: java.lang.Throwable: ABCD
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:16)
	... 1 more

Example: Getting Initial Cause of Exception

The following example shows the usage of java.lang.Throwable.initCause() 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 it which shows the initial cause.

package com.tutorialspoint;

public class ThrowableDemo {

   public static void main(String[] args) throws Throwable {
     try {
         raiseException();
      } catch(Exception e) {
         e.printStackTrace();
      }
   }
  
   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.Exception: This is new Exception...
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:15)
	at com.tutorialspoint.ThrowableDemo.main(ThrowableDemo.java:7)
Caused by: java.lang.Throwable: ABCD
	at com.tutorialspoint.ThrowableDemo.raiseException(ThrowableDemo.java:16)
	... 1 more

java_lang_throwable.htm
Advertisements