Throwable getStackTrace() method in Java with Examples
Last Updated :
26 Apr, 2023
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
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
addPositiveNumbers( 2 , - 1 );
}
catch (Throwable e) {
StackTraceElement[] stktrace
= e.getStackTrace();
for ( int i = 0 ; i < stktrace.length; i++) {
System.out.println("Index " + i
+ " of stack trace"
+ " array contains = "
+ stktrace[i].toString());
}
}
}
public static void addPositiveNumbers( int a, int b)
throws 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 contains = GFG.addPositiveNumbers(File.java:48)
Index 1 of stack trace array contains = GFG.main(File.java:18)
Example 2:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
throws Exception
{
try {
testException1();
}
catch (Throwable e) {
StackTraceElement[] stktrace
= e.getStackTrace();
for ( int i = 0 ; i < stktrace.length; i++) {
System.out.println("Index " + i
+ " of stack trace"
+ " array contains = "
+ stktrace[i].toString());
}
}
}
public static void testException1()
throws Exception
{
testException2();
}
public static void testException2()
throws Exception
{
testException3();
}
public static void testException3()
throws IndexOutOfBoundsException
{
throw new IndexOutOfBoundsException(
"Forcefully Generated Exception");
}
}
|
Output:
Index 0 of stack trace array contains = GFG.testException3(File.java:68)
Index 1 of stack trace array contains = GFG.testException2(File.java:58)
Index 2 of stack trace array contains = GFG.testException1(File.java:46)
Index 3 of stack trace array contains = GFG.main(File.java:17)
References: https://docs.oracle.com/javase/10/docs/api/java/lang/Throwable.html#getStackTrace()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...