Lesson 6.1
Syntax, run-time and logic errors
Part 6: Errors, testing and filesReading, about 6 min2 exercises in this lesson
Syntax, run-time and logic errors
By the end of this you will be able to tell a syntax error, a run-time error and a logic error apart, say when each one is found, and say what you see when one happens.
Every program you have written has gone wrong at some point. That is normal. What helps is knowing which kind of error you have, because each kind is found at a different time and is fixed in a different way.
Syntax errors: the rules of the language
Each language has rules for how code must be written, called its syntax: where brackets and quote marks go, how a line ends, how a block is marked. Here is a short program that follows the rules.
price = 4
count = 3
print("Total: " + str(price * count))public static void main(String[] args) {
int price = 4;
int count = 3;
System.out.println(
"Total: " + price * count);
}It prints Total: 12.
In Python, delete the quote mark after Total: , and Python gives the message SyntaxError: unterminated string literal (detected at line 3). In Java, delete the semicolon after int count = 3, and the Java compiler gives the message error: ';' expected, with the line number.
Either way, none of the program runs. Not even the first line runs, although the first line is fine. The whole program is checked before any of it starts, and a syntax error stops it at that check. In Java this check is called compiling, so a syntax error is one kind of compile error.
To fix a syntax error, go to the line the message names and look for a missing or extra bracket, quote mark or symbol. Sometimes the real slip is on the line just above.
Run-time errors: a line that cannot be carried out
A program with no syntax errors starts to run. A run-time error happens when it reaches a line that cannot be carried out with the values it has at that moment.
scores = [7, 9, 4]
i = 3
print("First: " + str(scores[0]))
print(scores[i])
print("Done")public static void main(String[] args) {
int[] scores = {7, 9, 4};
int i = 3;
System.out.println(
"First: " + scores[0]);
System.out.println(scores[i]);
System.out.println("Done");
}When it runs, this is printed first:
First: 7
Then the program stops at the next line, because the list (an array in Java) has only the indexes 0, 1 and 2, so there is no index 3. Python stops with IndexError: list index out of range. Java stops with java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3. The line before has already run, and Done is never printed.
You have met another run-time error already: text such as three turned into a whole number. A run-time error often depends on the values. With i set to 2, the same program runs to the end with no error. So a program can pass one test and stop with an error on another.
To fix a run-time error, read which line it stopped on and what the message says, then work out which value made that line impossible.
Logic errors: the wrong answer, with no message
A logic error is a mistake in what the program does, not in how it is written. The program runs to the end with no message at all, but the result is wrong.
This program is meant to print the average of 6 and 8, which is 7.
a = 6 b = 8 print(a + b // 2)
public static void main(String[] args) {
int a = 6;
int b = 8;
System.out.println(a + b / 2);
}It prints 10. Division comes before addition, so the program works out b divided by 2 first, which is 4, and then adds a. The code follows every rule, so there is no syntax error, and every line can be carried out, so there is no run-time error. The computer cannot know that you meant to add first. Brackets around a + b fix it.
A loop that never stops, as you saw in the lesson on updating a loop, is a logic error too.
Logic errors are often the hardest to find, because nothing points at them. You notice one only by working out the right answer yourself and comparing it with what the program gives. That is why testing matters, and the next lesson shows how to track one down.
The three kinds side by side
| Kind | When it is found | What you see |
|---|---|---|
| Syntax error | before any of the program runs | an error message naming a line, and nothing runs |
| Run-time error | while the program runs, at the line that cannot be carried out | the lines before it have run, then the program stops with an error message |
| Logic error | not by the computer: by you, when you test | the program runs with no error message, but the result is wrong or it never stops |
Where the two languages differ
Java checks more before a program runs. Each variable in Java has a type, and the compiler checks that every value fits its type. Python checks types only when a line runs.
In Java, int age = "16"; is a compile error, found before anything runs: error: incompatible types: String cannot be converted to int.
In Python, age = "16" is allowed. The trouble comes later, at a line such as age + 1, which stops the program with TypeError: can only concatenate str (not "int") to str. That is a run-time error, and every line before it has already run.
So the same slip can be a compile error in Java and a run-time error in Python.
Two common mistakes
- Thinking that no error message means the program is right. A logic error gives no message. Check the output against an answer you worked out yourself.
- Calling every error message a syntax error. If some of the program ran before the message appeared, it is a run-time error.
Now try it yourself on the questions below.
Exercise 6.1.16.1.1 · Answer itWhich kind of error
A student tests a quiz program that asks the player three questions and keeps the answers in a list. Something different goes wrong on three different days. For each of (a), (b) and (c), state whether the error is a syntax error, a run-time error or a logic error.
(a) When the student starts the program, it asks no questions at all. A message points at line 7, where a closing bracket is missing. [1]
(b) The program asks all three questions. Then it stops with an error message when it tries to read a fourth answer from the list. [1]
(c) The program asks all three questions and finishes with no message, but it says the player scored 2 out of 3 when every answer was right. [1]
(d) The student changes the program to fix the error in (c). Explain how the student could test whether it is now fixed. [2]
[5]