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.
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:
Index 0 of stack trace array conatins = GFG.addPositiveNumbers(File.java:48) Index 1 of stack trace array conatins = GFG.main(File.java:18)
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)
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.