12

What's the easiest way to print a stacktrace from a debugging printout? Often during testing you would like to know the callstack leading up to the situation provoking a debug message.

Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35

8 Answers8

20

If you're using log4j

Exception e = new Exception();
log.error("error here", e);

will print the stacktrace to your log.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
shsteimer
  • 28,436
  • 30
  • 79
  • 95
11

Thread.dumpStack();

Dan Dyer
  • 53,737
  • 19
  • 129
  • 165
  • 1
    The downside is that dumpStack() only goes to stderr, and you can't send it somewhere else like you can with an exception or Thread.getStackTrace(). – Paul Tomblin Sep 10 '08 at 18:58
  • 4
    Use logging. Printing to stdout/stderr should only be done by OS utilities and "Hello World" apps. – Chris Nava Nov 16 '09 at 17:23
6

If you want to save the stack trace into a String you can do this;

String exception = "";
for (StackTraceElement element : e.getStackTrace())
   exception += element.toString() + "\n";

Where e is, obviously, an exception.

Besides, it sounds very weird to autogenerate an own Exception just to find get a stack trace for a debug. Get Eclipse and use it's debug mode, it's really awesome.

4

Just creating an arbitrary exception does the trick for me:

System.out.println("Oops, the bad thing happened");
new IllegalStateException().printStackTrace();
Johan Lübcke
  • 19,666
  • 8
  • 38
  • 35
  • I believe that this method will cause a warning in some IDEs, such as IntelliJ, because you're creating an exception but not throwing it. I recommend the Thread.getStackTrace() method which has the same logic but it's hidden from the developer. – Tim Frey Sep 10 '08 at 18:15
  • Thread.dumpStack() is easier if you just want to send it to stderr (saves having to loop over the stack trace elements yourself). – Dan Dyer Sep 10 '08 at 18:29
  • 2
    This method is useful in JavaME which doesn't have getStackTrace or dumpStack methods on Thread. – izb Sep 10 '08 at 19:52
3

As well as what @jjnguy said, if you don't have an exception, you can also call Thread.getStackTrace().

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
2

To simply print the current stack trace to stderr, you can call:

Thread.dumpStack();

which itself just calls:

new Exception("Stack trace").printStackTrace();

To output to stdout rather than stderr, pass System.out to printStackTrace():

new Exception("Stack trace").printStackTrace(System.out);
minipif
  • 4,756
  • 3
  • 30
  • 39
2

You should be catching the exception in a try-catch block.

e.getStackTrace();

That returns StackTraceElement[] that you can then interpret.

Also:

e.printStackTrace()

will...print the stacktrace.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
1

Just because I needed it myself:

As inspired by answer How do I find the caller of a method using stacktrace or reflection? , you can retrieve the call stack using

StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace()

Then you process and print/log whatever you are interested in. More work than using Thread.dumpStack(), but more flexible.

Community
  • 1
  • 1
sleske
  • 81,358
  • 34
  • 189
  • 227