Reading stack traces like a detective
Top frame is where it threw; your package is where the bug probably is
A stack trace is the timeline of a failure. The top frame is the instruction that threw. Below it are the callers, in reverse order of execution. The trace usually contains a lot of library frames — scan downward for the first frame in your package. That is nearly always where the bad data entered the system.
java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null
at com.acme.billing.InvoiceFormatter.lineFor(InvoiceFormatter.java:42)
at com.acme.billing.InvoicePrinter.print(InvoicePrinter.java:19)
at com.acme.Main.main(Main.java:9)
Caused by: com.acme.data.CustomerNotFoundException: 7742
at com.acme.data.CustomerRepository.loadOrThrow(CustomerRepository.java:88)
... 2 moreCaused-by chains tell the whole story
When an exception is rethrown across a boundary with new X(msg, cause), the logs print a Caused by: chain. The innermost cause is usually the real fault. Readers who stop at the wrapper message miss the SQLException text that would have pointed at the bug.
Pair traces with the stack/heap story lab →
Takeaways
- Top frame = site of throw; first line in your package = most likely bug.
- Always preserve
cause— do not drop the original exception. - Log once; let exceptions propagate until the top layer decides what to do.
Enjoying This Lesson?
Your support helps create more comprehensive courses and lessons like this one. Help me build better learning experiences for everyone.
Support Awashyak