0 votes
1 view
ago in Java by (1.7k points)

How do I get the current stack trace in Java, like how in .NETyou can do Environment.StackTrace?

BTW, Thread.dumpStack() is not what I want - I want to get the stack trace back, not print it out.

1 Answer

0 votes
ago by (6.7k points)

To get a current stack trace in JAVA useThread.currentThread().getStackTrace()

Example:

for (StackTraceElement ste : Thread.currentThread().getStackTrace())

 {

    System.out.println(ste);

}

This Returns an array of StackTraceElement, each array represents one stack frame.

To learn more about it click here.

...