Skip to content
Related Articles

Related Articles

Throwable getStackTrace() method in Java with Examples
  • Last Updated : 19 Aug, 2019

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:






// 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);
        }
    }
}
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:




// 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");
    }
}
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()

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :