Throwable initCause() method in Java with Examples

The initCause() method of Throwable class is used to initialize the cause of the this Throwable with the specified cause passed as a parameter to initCause(). Actually, the cause is the throwable that caused this throwable Object to get thrown when an exception occurs. This method can be called only once. Generally, This method is called from within the constructor, or immediately after creating the throwable. If the calling Throwable is created by using Throwable(Throwable) or Throwable(String, Throwable), then this method cannot be called even once.

Syntax:

public Throwable initCause?(Throwable cause)

Parameters: This method accepts cause as a parameter which represents the cause of the this Throwable.

Returns: This method returns a reference to this Throwable instance.

Exception: This method throws:



  • 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.

Below programs illustrate the initCause method of Throwable class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the initCause() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            testException1();
        }
  
        catch (Throwable e) {
  
            System.out.println("Cause : "
                               + e.getCause());
        }
    }
  
    // method which throws Exception
    public static void testException1()
        throws Exception
    {
  
        // ArrayIndexOutOfBoundsException Exception
        // This exception will be used as a Cause
        // of another exception
        ArrayIndexOutOfBoundsException
            ae
            = new ArrayIndexOutOfBoundsException();
  
        // create a new Exception
        Exception ioe = new Exception();
  
        // initialize the cause and throw Exception
        ioe.initCause(ae);
  
        throw ioe;
    }
}

chevron_right


Output:

Cause : java.lang.ArrayIndexOutOfBoundsException

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the initCause() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            // add the numbers
            addPositiveNumbers(2, -1);
        }
        catch (Throwable e) {
  
            System.out.println("Cause : "
                               + e.getCause());
        }
    }
  
    // method which adds two positive number
    public static void addPositiveNumbers(int a, int b)
        throws Exception
    {
  
        // if Numbers are Positive
        // than add or throw Exception
        if (a < 0 || b < 0) {
  
            // create a Exception
            // when Numbers are not Positive
            // This exception will be used as a Cause
            // of another exception
            Exception
                ee
                = new Exception("Numbers are not Positive");
  
            // create a new Exception
            Exception anotherEXe = new Exception();
  
            // initialize the cause and throw Exception
            anotherEXe.initCause(ee);
  
            throw anotherEXe;
        }
  
        else {
  
            System.out.println(a + b);
        }
    }
}

chevron_right


Output:

Cause : java.lang.Exception: Numbers are not Positive

Refrences: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#initCause(java.lang.Throwable)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

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.