O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Stack walking for improved debugging

Stack trace can be very helpful in figuring out the source of the problem, although the need to read it is usually caused by some unpleasant surprise. Once in a while, especially in a big and complex system, the need arises to read it programmatically when an automatic correction is possible.

Since Java 1.4, the current stack trace can be accessed via the java.lang.Thread and java.lang.Throwable classes. To any method of your code, you can add the following line:

Thread.currentThread().dumpStack();

You can also add the following line:

new Throwable().printStackTrace();

It will print the stack trace to the standard output. Alternatively, since Java 8, you can use any of the following lines for the same ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required