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:
You can also add the following line:
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 ...