Reading the message
After this: Read the short form, the full Python traceback and the Java stack trace, and find the failing line.
On this page · 6 sections · 3 checks
A note comes through the door: "Not delivered. Nobody home. 14 Oak Street, 10:15." It says what happened, why, and where. An is the same kind of note from a program. Lesson 1 read the first two parts, the kind and the detail. This lesson reads the where.
The short form
When you press Run, the block shows the message in one line: line 4: ZeroDivisionError: division by zero. Three parts: the line number, the name of the kind of problem, and the detail. The line number says where to look in your code. The name is the part to search the web for when you do not know what it means.
That is enough when the program has no functions. With functions, where is a path: the failing line is inside a function, which was called from another line, which may be inside another function. The full message lists that whole path.
A failure three functions deep
show calls report, report calls average, and average divides. The program calls show(0), so the count that reaches the division is 0.
- The program calls
show(0): one frame opens. showcallsreport(0): two frames.reportcallsaverage(100, 0): three frames.- The division line inside
averagefails. At that moment four frames are open: the program,show,reportandaverage. The band shows all four beside the message.
- Run the programRun it and step to the end. At the failing line, count the frames in the Variables panel: the program, show, report and average are all open. That path is what the full message lists.
def average(total, count): return total / count
def report(count): return average(100, count)
def show(count): print(report(count))
show(0)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
The division did nothing wrong on its own: 100 divided by a count is a fine line. The 0 came from the program's call. A message lists the whole path for that reason: the line that failed is at one end, and the line that chose the bad value is often somewhere else along it.
The full message
Here is the same program with everything the screen shows. Python and Java print the path in opposite orders, so read the note for your language.
def average(total, count): return total / count
def report(count): return average(100, count)
def show(count): print(report(count))
show(0)Traceback (most recent call last):
File "main.py", line 10, in <module>
show(0)
~~~~^^^
File "main.py", line 8, in show
print(report(count))
~~~~~~^^^^^^^
File "main.py", line 5, in report
return average(100, count)
File "main.py", line 2, in average
return total / count
~~~~~~^~~~~~~
ZeroDivisionError: division by zeropublic class Main { static int average(int total, int count) { return total / count; }
static int report(int count) { return average(100, count); }
static void show(int count) { System.out.println(report(count)); }
public static void main(String[] args) { show(0); }}Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.average(Main.java:3) at Main.report(Main.java:7) at Main.show(Main.java:11) at Main.main(Main.java:15)
In both languages the frame next to the name of the problem is where it failed, and the entry at the far end is where the program started. The frames between them are the path, and the bad value can come from any of them.
Run it
The short form gives the innermost line, line 2. Change the 0 in show(0) to a 4 and run again: the program prints 25.025, the average of 100 over 4.
Check yourself
Read the message under the code. On which line did the program fail, and inside which function?
def average(total, count): return total / count
def report(count): return average(100, count)
def show(count): print(report(count))
show(0)Traceback (most recent call last):
File "main.py", line 10, in <module>
show(0)
~~~~^^^
File "main.py", line 8, in show
print(report(count))
~~~~~~^^^^^^^
File "main.py", line 5, in report
return average(100, count)
File "main.py", line 2, in average
return total / count
~~~~~~^~~~~~~
ZeroDivisionError: division by zeropublic class Main { static int average(int total, int count) { return total / count; }
static int report(int count) { return average(100, count); }
static void show(int count) { System.out.println(report(count)); }
public static void main(String[] args) { show(0); }}Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.average(Main.java:3) at Main.report(Main.java:7) at Main.show(Main.java:11) at Main.main(Main.java:15)
What does this show?
text = "ten"number = int(text)print(number * 2)public class Main { public static void main(String[] args) { String text = "ten"; int number = Integer.parseInt(text); System.out.println(number * 2); }}A full message lists the path of calls, one entry per open frame. Where in that list is the function that failed, in a Python tracebacka Java stack trace?
Honesty notes
- Read the short form of an error message: the line number, the name of the kind of problem, and a detail.
- Read a Python traceback from the bottom: the last line names the problem, and the entries above it are the path of calls, newest last.
- Read a Java stack trace from the top: the first line names the problem, and the at lines below it are the path of calls, newest first.
- Find the line and the function where a program failed, and say that the bad value may have come from a caller.