Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Java by (1.3k points)

How do I get the current stack trace in Java, like how in .NET you 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
by (13.2k points)

Knowing the current stack trace is very useful in java. It helps in debugging and troubleshooting problems.

As you don’t want the stack trace directly printed out the best way is to use 

Thread.currentThread().getStackTrace() function.

This function returns an array of  current stack trace elements of this thread. The first element of the array i.e. top of the stack is the most recent method invocation in the sequence. The last element of the array i.e. bottom of the stack is the least recent method invocation in the sequence.

If this thread has not started, or has been terminated then this method will return a zero-length array.

Related questions

0 votes
1 answer
asked Jul 9, 2019 in Java by Nigam (4k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...