Debugging Basics: Compiler Errors vs. Exceptions
Prerequisites: Comments & code style
Java surfaces problems in two very different ways. A compiler
error (from javac) means the code doesn't even compile — a typo, a
type mismatch, a missing semicolon — and no .class file is
produced at all. An exception happens while a successfully
compiled program is running — dividing by zero, indexing past an
array's end, calling a method on null.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 3 out of bounds for length 3
at Main.main(Main.java:4)
Read a stack trace from the top down: the exception type and message come first, then the exact file and line where it happened. Set a breakpoint on that line in your IDE's debugger to inspect variable values at the moment things went wrong.
Common mistakes
- Reading only the first line of a long stack trace and missing the actual line number where the failure occurred, further down in "Caused by:" sections.