Java Thread getStackTrace() method

The getStackTrace() method of thread class returns an array of stack trace elements representing the stack dump of the thread. The first element of an array represents the top of the stack which is the last method invocation in the sequence. The last element of the array represents the bottom of the stack which is the first method invocation in the sequence.

Syntax

Return

It is an array of StackTraceElement, each represents one stack frame.

Exception

SecurityException:This exception throws if a security manager exists and its checkPermission method doesn't allow getting the stack trace of the thread.

Example

Test it Now

Output:

Displaying Stack trace using StackTraceElement in Java
java.lang.Thread.getStackTrace(Thread.java:1559)
JavaGetStackTraceExp.third(JavaGetStackTraceExp.java:17)
JavaGetStackTraceExp.second(JavaGetStackTraceExp.java:13)
JavaGetStackTraceExp.first(JavaGetStackTraceExp.java:9)
JavaGetStackTraceExp.main(JavaGetStackTraceExp.java:5)





Latest Courses