Throwable getStackTrace() method in Java with Examples

The getStackTrace() method of Throwable class used to return an array of stack trace elements which is the stack trace information printed by printStackTrace(). In the array of stack trace elements(assuming the array’s length is non-zero), each element represents one stack frame. The first element of the array means zeroth index element of this array represents the top of the stack, which is the last method invoked in the sequence or we can say that this zeroth index element information is related to the point where throwable was created and thrown. The last element of this array represents the bottom of the stack, which is the first method invoked in the sequence.
In Some cases one or more stack frames from the stack trace is returned. The array returned by this method will contain one element for every frame that would be printed by printStackTrace. Any changes to the returned array do not affect future calls to this method.

Syntax:

public StackTraceElement[] getStackTrace()

Returns: This method returns an array of stack trace elements representing the stack trace information.



Below programs illustrate the getStackTrace method of Throwable class:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the getStackTrace() 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) {
  
            // get StackTraceElements
            // using getStackTrace()
            StackTraceElement[] stktrace
                = e.getStackTrace();
  
            // print element of stktrace
            for (int i = 0; i < stktrace.length; i++) {
  
                System.out.println("Index " + i
                                   + " of stack trace"
                                   + " array conatins = "
                                   + stktrace[i].toString());
            }
        }
    }
  
    // 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) {
  
            throw new Exception(
                "Numbers are not Positive");
        }
  
        else {
  
            System.out.println(a + b);
        }
    }
}

chevron_right


Output:

Index 0 of stack trace array conatins = GFG.addPositiveNumbers(File.java:48)
Index 1 of stack trace array conatins = GFG.main(File.java:18)

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the getStackTrace() Method.
  
import java.io.*;
  
class GFG {
  
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
  
        try {
  
            testException1();
        }
  
        catch (Throwable e) {
  
            // get StackTraceElements
            // using getStackTrace()
            StackTraceElement[] stktrace
                = e.getStackTrace();
  
            // print element of stktrace
            for (int i = 0; i < stktrace.length; i++) {
  
                System.out.println("Index " + i
                                   + " of stack trace"
                                   + " array conatins = "
                                   + stktrace[i].toString());
            }
        }
    }
  
    // method which throws Exception
    // calling other method testException2
    public static void testException1()
        throws Exception
    {
        // This method second in series
        // of calling method which throw exception
        // so this will be second index element
        testException2();
    }
  
    // method which throws Exception
    // calling other method testException3
    public static void testException2()
        throws Exception
    {
  
        // This method calls a method
        // where exception is thrown
        // so this will be first index element
        testException3();
    }
  
    // method which throws IndexOutOfBoundsException
    public static void testException3()
        throws IndexOutOfBoundsException
    {
  
        // here exception thrown
        // so this will be Zeroth element
        throw new IndexOutOfBoundsException(
            "Forcefully Generated Exception");
    }
}

chevron_right


Output:

Index 0 of stack trace array conatins = GFG.testException3(File.java:68)
Index 1 of stack trace array conatins = GFG.testException2(File.java:58)
Index 2 of stack trace array conatins = GFG.testException1(File.java:46)
Index 3 of stack trace array conatins = GFG.main(File.java:17)

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



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.



Improved By : Akanksha_Rai