Android / Java – An example of how to use printStackTrace()
by Riley MacDonald, October 16, 2017
I’m a big fan of using the debugger to diagnose issues by using the evaluation, stack and thread inspection tools. Unfortunately this method doesn’t always work, especially when dealing with multi-threading, concurrency or race conditions. It can be valuable to print the stack trace when troubleshooting the error. At any time you can take advantage of the printStackTrace()
method of Throwable to print a stack trace up to the desired point in code.
Example
Create a new Throwable
and invoke printStackTrace()
.
1 2 | Throwable throwable = new Throwable(); throwable.printStackTrace(); |
Check your console / logcat for the output. If you’re using an IDE such as IntelliJ you can even click the classes/methods to automatically navigate to them.