Throwable initCause(Throwable) in Java

initCause(Throwable): This method is available in the java.lang.Throwable class of Java.

Syntax:

Throwable java.lang.Throwable.initCause(Throwable cause)

This method takes one argument. This method initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.)

This method can be called at most once. It is generally called from within the constructor, or immediately after creating the throwable. If this throwable was created with Throwable(Throwable) or Throwable(String, Throwable), this method cannot be called even once.

Parameters: One parameter is required for this method.

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

Returns: a reference to this Throwable instance.

Throws:

1. IllegalArgumentException - if the cause is this throwable. (A throwable cannot be its own cause.)

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

Approach 1: When no exception

Java

public class ThrowableinitCause {
    public static void main(String[] args) {

        Throwable cause = new NullPointerException();
        Throwable throwable = new Throwable();

        System.out.println(throwable.initCause(cause));
    }
}

Output:

java.lang.Throwable


Approach 2: IllegalArgumentException 

Java

package com.Throwable;

public class ThrowableinitCause {
    public static void main(String[] args) {

        Throwable throwable = new Throwable();

        System.out.println(throwable.initCause(throwable));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Self-causation not permitted at java.base/java.lang.Throwable.initCause(Throwable.java:466) at com.Throwable.ThrowableinitCause.main(ThrowableinitCause.java:8) Caused by: java.lang.Throwable at com.Throwable.ThrowableinitCause.main(ThrowableinitCause.java:6)


Approach 3: IllegalStateException 

Java

package com.Throwable;

public class ThrowableinitCause {
    public static void main(String[] args) {

        String message = "Hello";
        Throwable cause = new NullPointerException();
        Throwable throwable = new Throwable(message, cause);

        System.out.println(throwable.initCause(cause));
    }
}

Output:

Exception in thread "main" java.lang.IllegalStateException: Can't overwrite cause with java.lang.NullPointerException at java.base/java.lang.Throwable.initCause(Throwable.java:464) at com.Throwable.ThrowableinitCause.main(ThrowableinitCause.java:10) Caused by: java.lang.Throwable: Hello at com.Throwable.ThrowableinitCause.main(ThrowableinitCause.java:8) Caused by: java.lang.NullPointerException at com.Throwable.ThrowableinitCause.main(ThrowableinitCause.java:7)


Some other methods of Throwable

Throwable()Constructs a new throwable with null as its detail message.

Throwable(String)This method constructs a new throwable with the specified detail message.

Throwable(Throwable)This method constructs a new throwable with the specified cause and a detailed message of (cause==null ? null: cause.toString()) (which typically contains the class and detailed message of cause).

Throwable(String, Throwable)This method constructs a new throwable with the specified detail message and cause.

addSuppressed(Throwable)This method appends the specified exception to the exceptions that were suppressed in order to deliver this exception.

fillInStackTrace()This method records within this Throwable object information about the current state of the stack frames for the current thread.

getCause()Returns the cause of this throwable or null if the cause is nonexistent or unknown. (The cause is the throwable that caused this throwable to get thrown.)

getLocalizedMessage()Creates a localized description of this throwable.

getMessage()Returns the detailed message string of this throwable.

getStackTrace()Returns an array of stack trace elements, each representing one stack frame.

getSuppressed()Returns an array containing all of the exceptions that were suppressed, typically by the try-with-resources statement, in order to deliver this exception.

initCause(Throwable)This method initializes the cause of this throwable to the specified value. (The cause is the throwable that caused this throwable to get thrown.)

printStackTrace()Prints this throwable and its backtrace to the standard error stream.

printStackTrace(PrintStream)This method prints this throwable and its backtrace to the specified print stream.

printStackTrace(PrintWriter)This method prints this throwable and its backtrace to the specified print writer.

setStackTrace(StackTraceElement[])his method sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods.

toString()Returns a short description of this throwable.

No comments:

Post a Comment