Throwable getStackTrace() method in Java with Examples - GeeksforGeeks

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);        }    }}
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");    }}
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()
Akanksha_Rai
Java-Exception Handling
Java-Exceptions
Java-Functions
Java-lang package
java-Throwable
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Multidimensional Arrays in Java
Singleton Class in Java