Throwable initCause() method in Java with Examples
The initCause() method of Throwable class is used to initialize the cause of this Throwable with the specified cause passed as a parameter to initCause(). Actually, the cause is 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.
public Throwable initCause?(Throwable cause)
Parameters: This method accepts cause as a parameter which represents the cause of 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:
Cause : java.lang.ArrayIndexOutOfBoundsException
Cause : java.lang.Exception: Numbers are not Positive
Please Login to comment...