Throwable setStackTrace() method in Java with Examples

The setStackTrace(StackTraceElement[] stackTrace) method of Throwable class is used to Set the stack trace elements to this throwable object and this stack trace will be returned by getStackTrace() and printed by printStackTrace() and related methods. This method allows the user 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.
If the stack trace of any Throwable is not writable then calling this method has no effect other than validating its argument.

Syntax:

public void 
    setStackTrace(StackTraceElement[] stackTrace)

Parameters: This method accepts only one parameter stackTrace which is the stack trace elements to be associated with this Throwable.

Returns: This method does not returns abything.

Below programs illustrate the setStackTrace() method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the setStackTrace () Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            testException1();
        }
  
        catch (Throwable e) {
  
            // access to the stack trace
            StackTraceElement[] trace = e.getStackTrace();
  
            System.out.println(trace[0].toString());
        }
    }
  
    // method which throws Exception
    public static void testException1()
        throws Exception
    {
  
        // create a new Exception
        Exception ex = new Exception();
  
        StackTraceElement[] trace = new StackTraceElement[] {
            new StackTraceElement("ClassNameOfExe",
                                  "methodNameOfExe",
                                  "fileNameOfExe",
                                  10)
        };
  
        // sets the stack trace elements
        ex.setStackTrace(trace);
  
        // throw the Throwable[
        throw ex;
    }
}

chevron_right


Output:

ClassNameOfExe.methodNameOfExe(fileNameOfExe:10)

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the setStackTrace () Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            Exceptiontest();
        }
  
        catch (Throwable e) {
  
            // access to the stack trace
            StackTraceElement[] trace = e.getStackTrace();
  
            System.out.println("StackTraceElement length :"
                               + trace.length);
  
            for (int i = 0; i < trace.length; i++) {
  
                System.out.println("Stack Trace at index "
                                   + i + " : "
                                   + trace[i]);
            }
        }
    }
  
    // method which throws Exception
    public static void Exceptiontest()
        throws Exception
    {
  
        // create a new Exception
        ArrayStoreException ex = new ArrayStoreException();
  
        StackTraceElement[] trace = new StackTraceElement[] {
            new StackTraceElement("ClassName1", "methodName1",
                                  "fileName1", 10),
            new StackTraceElement("ClassName2", "methodName2",
                                  "fileName2", 20),
            new StackTraceElement("ClassName3", "methodName3",
                                  "fileName3", 14)
        };
  
        // sets the stack trace elements
        ex.setStackTrace(trace);
  
        throw ex;
    }
}

chevron_right


Output:

StackTraceElement length :3
Stack Trace at index 0 : ClassName1.methodName1(fileName1:10)
Stack Trace at index 1 : ClassName2.methodName2(fileName2:20)
Stack Trace at index 2 : ClassName3.methodName3(fileName3:14)

References:
https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#setStackTrace(java.lang.StackTraceElement%5B%5D)



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.