Throwable setStackTrace(StackTraceElement[]) in Java

setStackTrace(StackTraceElement[]): This method is available in the java.lang.Throwable class of Java.

Syntax:

void java.lang.Throwable.setStackTrace(StackTraceElement[] stackTrace)

This method takes one argument. This method sets the stack trace elements that will be returned by getStackTrace() and printed by printStackTrace() and related methods.

This method, which is designed for use by RPC frameworks and other advanced systems, allows the client to override the default stack trace that is either generated by fillInStackTrace() when a throwable is constructed or deserialized when a throwable is read from a serialization stream.

Parameters: One parameter is required for this method.

stackTrace: the stack trace elements to be associated with this Throwable. The specified array is copied by this call; changes in the specified array after the method invocation returns will have no effect on this Throwable's stacktrace.

Returns: NA

Throws:

1. NullPointerException - if stackTrace is null or if any of the elements of stackTrace are null.

Approach 1: When no exception

Java

import java.util.Arrays;

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

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

        StackTraceElement stackTrace =
new StackTraceElement(message, message, message, 0);

        StackTraceElement[] stackTraceElements = { stackTrace };
        throwable.setStackTrace(stackTraceElements);
        System.out.println(Arrays.toString(throwable.getStackTrace()));
    }
}

Output:

[Hello.Hello(Hello:0)]


Approach 2: NullPointerException 

Java

package com.Throwable;

import java.util.Arrays;

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

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

        StackTraceElement[] stackTraceElements = null;
        throwable.setStackTrace(stackTraceElements);
        System.out.println(Arrays.toString(throwable.getStackTrace()));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "[Ljava.lang.StackTraceElement;.clone()" because "stackTrace" is null at java.base/java.lang.Throwable.setStackTrace(Throwable.java:877) at com.Throwable.ThrowablesetStackTrace.main(ThrowablesetStackTrace.java:13)


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