Sub topic 8
Reading a Stack Trace
When an exception is not caught, Java stops the program and prints a report. This report is called a stack trace. A stack trace is a list of the method calls that were running when the exception happened. It tells you what went wrong and where it went wrong.
Many beginners skip past this wall of text, but it is one of the best debugging tools you have. You can learn to read one in a few minutes.
The Call Stack
To read a stack trace, you need to know about the call stack. The call stack is the list of methods that are running right now. Each entry in it is called a frame. When main calls report, Java puts a frame for report on top of the frame for main. When report calls average, a frame for average goes on top of that.
When an exception is thrown, Java takes a snapshot of this stack. That snapshot is the stack trace.
A Worked Example
This program tries to average an empty array. Dividing an integer by zero throws an ArithmeticException.
public class TraceDemo {
static int average(int[] values) {
int sum = 0;
for (int v : values) {
sum += v;
}
return sum / values.length;
}
static void report(int[] values) {
System.out.println("Average: " + average(values));
}
public static void main(String[] args) {
report(new int[] {});
}
}Save it as TraceDemo.java, then compile and run it. You will see this output:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at TraceDemo.average(TraceDemo.java:7)
at TraceDemo.report(TraceDemo.java:11)
at TraceDemo.main(TraceDemo.java:15)Reading It Line by Line
The first line names the exception and gives its message.
| Part | Meaning |
|---|---|
| `"main"` | The thread that failed. Simple programs have only the `main` thread. |
| `java.lang.ArithmeticException` | The full name of the exception class. |
| `/ by zero` | The message, which describes the problem. |
Each line that starts with at is one frame. It has this pattern:
at ClassName.methodName(FileName.java:lineNumber)The top frame is where the exception was thrown: line 7 of TraceDemo.java, inside average. Each frame below it is the caller of the frame above. So report called average on line 11, and main called report on line 15.
Printing a Trace Yourself
If you catch an exception, you can still print its stack trace by calling printStackTrace() on it:
public class CatchTrace {
public static void main(String[] args) {
try {
int[] data = new int[2];
data[5] = 1;
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
System.out.println("Still running");
}
}The trace goes to the error output, and the program keeps running.
Exercise
A program prints this trace. Which method threw the exception, on which line, and which method called it? Exception in thread "main" java.lang.NullPointerException at Shop.total(Shop.java:22) at Shop.checkout(Shop.java:14) at Shop.main(Shop.java:5)
Show answer
The top frame shows the throw site: the total method threw the exception on line 22 of Shop.java. The next frame down shows that checkout called total on line 14.
Exercise
In TraceDemo, what change to main would stop the exception from happening?
Show answer
Pass a non-empty array, such as report(new int[] {2, 4, 6}). Then values.length is not zero and the program prints "Average: 4".