Programs break
After this: Watch three programs stop, and say which lines ran and what the message names.
On this page · 7 sections · 3 checks
You follow a recipe. Step four says "cut the bread", and there is no bread. You stop at step four and say so. Steps one to three are done; step five never starts. A program does the same. When a line asks for something that cannot be done, the program stops at that line and prints a message that says what went wrong and where.
This tutorial is about those messages, about catching the problems you expect, and about finding the mistakes that give no message at all. A mistake in a program is a . Every programmer makes bugs, every day. The skill is finding them fast, and the message is the first tool for that.
Three errors on purpose
Below are three short programs, each with one bug put in on purpose. Step through each one and watch what the trace band does on the failing line.
Dividing by zero
- Lines 1 and 2 fill two boxes:
aholds 10 andbholds 0. - Line 3 prints
Start, as any print line would. - Line 4 asks for 10 divided by 0. That has no answer, so the line cannot run. The program stops, and the band shows the message beside the line: ZeroDivisionError: division by zerojava.lang.ArithmeticException: / by zero.
- Line 5 never runs.
Endis not printed.
- Run the programRun it and step through. Line 3 prints Start as usual. Then watch what the band does on the division line: the message appears beside the code, and the program stops.
a = 10b = 0print("Start")print(a / b)print("End")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
Text that is not a number
Line 2 tries to turn the text abc into a whole number. There is no number in it, so the line fails: ValueError: invalid literal for int() with base 10: 'abc'java.lang.NumberFormatException: For input string: "abc". The detail shows the text that was given.
text = "abc"number = int(text)print(number + 1)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
A position that does not exist
marks holds three values, at positions 0, 1 and 2. Line 2 asks for position 1 and prints 85. Line 3 asks for position 5, which does not exist, and fails: IndexError: list index out of rangejava.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 3.
marks = [72, 85, 60]print(marks[1])print(marks[5])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 rule
A program runs one line at a time. The lines before the failing one ran as usual: what they printed is on the screen, and the boxes they filled hold their values. The failing line stops the program. The lines after it never run.
The message has two parts. First comes a name for the kind of problem, and then a detail about this one. The name is the same every time that kind of problem happens; the detail changes. The next lesson reads a whole message, line by line.
The code
a = 10b = 0print("Start")print(a / b)print("End")Start
Traceback (most recent call last):
File "main.py", line 4, in <module>
print(a / b)
~~^~~
ZeroDivisionError: division by zeropublic class Main { public static void main(String[] args) { int a = 10; int b = 0; System.out.println("Start"); System.out.println(a / b); System.out.println("End"); }}Start Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:6)
The output has more lines than the two parts. The last linefirst line holds the name and the detail; the rest says where the program was, and the next lesson reads it. In this tutorial some programs have functions, so every Java block shows the whole file, as in "Functions".
Run it
Python runs in the browser when you press Run on the block below. Java does not run here: if you are learning Java, step through the trace above instead, and paste the code into a Java tool on your computer when you want to run it for real.
The Run block shows the message in a short form: the line number, the name and the detail, on one line. Change the 0 on line 24 to a 2 and run again: now every line runs, and 5.05 is printed between Start and End.
Four names you will meet
| What went wrong | Python | Java |
|---|---|---|
| Dividing by zero | ZeroDivisionError | ArithmeticException |
| Text that is not a number | ValueError | NumberFormatException |
| A list position that does not exist | IndexError | IndexOutOfBoundsException |
| A dictionary key that is not there | KeyError | no stop: get hands back null |
The last row is where the two languages differ. Python stops at once on the line that asks for the missing key. Java's get hands back null, the value that means "nothing here", and the program carries on. It fails later, on the line that tries to use that nothing as a number, with a NullPointerException. The line named in the message is then not the line with the mistake.
ages = {"Ana": 30, "Bo": 25}age = ages["Cy"]print(age + 1)Traceback (most recent call last):
File "main.py", line 2, in <module>
age = ages["Cy"]
~~~~^^^^^^
KeyError: 'Cy'import java.util.HashMap;
public class Main { public static void main(String[] args) { HashMap<String, Integer> ages = new HashMap<>(); ages.put("Ana", 30); ages.put("Bo", 25); Integer age = ages.get("Cy"); System.out.println(age + 1); }}Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local2>" is null at Main.main(Main.java:9)
The message names line 2, the line with the mistake.The message names line 9, the print, while the mistake is on line 8, where get handed back null. In "Dictionaries and sets" you saw get(key, default), which gives a value of your choice instead of stoppingthat get hands back null for a missing key, so ask whether the key is there before you use the value.
Check yourself
A program has five lines. Line 3 prints Start, line 4 divides by zero, and line 5 prints End. What appears on the screen?
Three names, positions 0, 1 and 2. What does this show?
names = ["Ana", "Bo", "Cy"]print(names[0])print(names[3])import java.util.ArrayList;import java.util.List;
public class Main { public static void main(String[] args) { ArrayList<String> names = new ArrayList<>(List.of("Ana", "Bo", "Cy")); System.out.println(names.get(0)); System.out.println(names.get(3)); }}A program runs int("abc")Integer.parseInt("abc"). Which message stops it?
Honesty notes
- Say what a bug is: a mistake in a program, and something every programmer makes.
- Say what happens on a failing line: the lines before it ran, the program stops there, and the lines after it never run.
- Name the kind of problem for dividing by zero, for text that is not a number, and for a list position that does not exist, in Python and in Java.
- Say that a missing dictionary key stops Python at once, while Java's get hands back null and the program fails later, when it uses that null.