Get current stack trace in Java

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.


You can use Thread.currentThread().getStackTrace() .

That returns an array of StackTraceElement s that represent the current stack trace of a program.


Thread.currentThread().getStackTrace();

is fine if you don't care what the first element of the stack is.

new Throwable().getStackTrace();

will have a defined position for your current method, if that matters.


for (StackTraceElement ste : Thread.currentThread().getStackTrace()) {
    System.out.println(ste);
}
链接地址: http://www.djcxy.com/p/2334.html

上一篇: C ++中正确的堆栈和堆使用情况?

下一篇: 用Java获取当前堆栈跟踪