Why Is Java Not Showing Full Stacktrace

Few weeks ago I got a recommendation from a colleague to read Effective Java. It’s insightful book with many great tips, some of which I’ve been using by default because IntelliJ is hinting me to do that, however most of tips were new to me.

One paragraph stated that “exceptions are expensive operations to throw, because they require the whole stacktrace to be captured”. It seems logical and that’s something I remembered. Today, it clicked to me that in production, when viewing logs, I see only the first line of the exception. i.e:

java.lang.NullPointerException

You can find a lot of questions on stackoverflow, like NullPointerException in Java with no StackTrace. To fix the “issue”, you can use -XX:-OmitStackTraceInFastThrow, but question is is that really what you want.

The compiler in the server VM now provides correct stack backtraces for all “cold” built-in exceptions. For performance purposes, when such an exception is thrown a few times, the method may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. To disable completely the use of preallocated exceptions, use this new flag: -XX:-OmitStackTraceInFastThrow.

Copied from stackoverflow

Further reading:


You can follow me on twitter, contact me on LinkedIn, or write me an email.