How to dump the stack trace into IntelliJ console (not log file)?

I am trying to print the stack trace for a passive breakpoint (suspend = none) and not able to. The eval expr field expects a string. If I put printStacktrace or Thread.dumpStack(), both return void so the ST won't show up in the console. Short of creating a static utility method that returns the ST as a string, is there a more elegant way to dump the ST in the console in IntelliJ ?

Screen Shot 2012-08-24 at 2.46.17 PM.png

1 comment
Comment actions Permalink

I do not believe there is a built in way to do this. I can suggest the following two options.

Option 1
You could Print the value (which is an array) of the getStackTrace() method. The Log Evaluated Expression field will handle the a Sting array as well as a String.

new RuntimeException().getStackTrace()

The downside here is that the stack trace will be comma delimited single line:

[com.example.MyExample.someMethod(, com.example.MyExample.main(]

This can make it much harder to read, especially on a long stack trace. And you do not get the exception's message, just the stack trace. Given your use case, the lost of the message is likely not a big deal.

As an FYI, if you want a String value so you can concatenate to another message, you could always wrap it in a call to Arrays.toString()

"My additional message: " + Arrays.toString(new RuntimeException().getStackTrace())

Option 2
To get a "standard" stack trace as a single string, you can use the getStackTrace() method of the ExceptionUtils class from the Apache Commons Lang library:

ExceptionUtils.getStackTrace(new RuntimeException())

That would result in a conventional stack trace output:

    at com.example.MyExample.someMethod(
    at com.example.MyExample.main(

That would of course mean you need to have the Commons Lang library as a dependency. Given its usefulness, and compact size, that is hopefully not an issue.

I hope that helps.


Please sign in to leave a comment.