Errors: syntax, runtime and logic

Reading an error message, and the three kinds of error by their exam names.

F1.4Programming basicsGCSE15 min

Covers OCR J277 2.3.2 Testing · AQA 8525 3.2.11 Robust and secure programming · Edexcel 1CP2 1.2.5 Types of error

Do this lesson in the simulator

Every programmer, on every day of their life, makes mistakes and reads error messages. Getting comfortable with that early is the most useful thing in this module. An error message is not the computer telling you off. It is the computer telling you where to look.

There are three kinds of mistake, and they feel very different: one stops the program before it starts, one stops it half way, and one never stops it at all.

Read the whole message

Run this. It has a mistake on purpose.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("starting")
prin("hello")
print("finished")

Run this in the simulator

Two things to notice. "starting" was printed, then the error appeared, and "finished" was not printed. A program stops at the first error it hits.

Now read the red text: NameError: name 'prin' is not defined. It tells you the kind of problem (a name Python does not know), what it was (prin), and where it happened. Fix the spelling and run again.

Syntax errors: Python cannot read it

The syntax of a language is its rules of grammar: brackets that close, quotes that end, colons in the right places. Break a rule and Python cannot understand the program at all, so none of it runs, not even the lines before the mistake:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("this line is fine")
print("missing bracket"

Run this in the simulator

SyntaxError. Also try print("missing quote). Syntax errors often point at the end of the line or the line after the real mistake, because that is where Python finally gave up.

Runtime errors: it crashes while running

A runtime error happens when a line that follows the rules cannot be carried out. The program starts, runs up to that line, then crashes:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

led("green")
print("waiting...")
wait("two")
led("off")

Run this in the simulator

The LED went green and "waiting..." was printed, so the program did run. Then wait was given the text "two" when it needs a number of seconds, and the program stopped with a TypeError. The LED never went off.

NameError, TypeError, ValueError and ZeroDivisionError are all runtime errors: Python only finds them when it reaches the line.

Logic errors: no message at all

The hardest kind. The program runs to the end with no error, and does the wrong thing. This program should drive the robot 15 cm up the mat, away from you:

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

print("driving up the mat")
backward(50, distance=15)
print("arrived")

Run this in the simulator

No red text, and it even printed "arrived". But the robot went the wrong way. Python did exactly what the program said; the program said the wrong thing. The only way to find a logic error is to test: know what should happen, watch what does, and compare. Watching the robot is part of debugging.

The three example programs as rows of instructions: the syntax error runs none, the runtime error runs up to its bad line, the logic error runs them allsyntax errorprintprintnothing runs at allruntime errorledprintwaitledstops half waylogic errorprintbackwardprintevery line ran,no messageruns to the end, wrong resultranthe mistake, and the errornever ran
The three example programs above, one box for each line after connect(). A syntax error stops everything before it starts, a runtime error lets the lines before it run, and a logic error lets every line run and gives no message at all.

How to fix things

  1. Read the error. Find the kind and the line number.
  2. Look at that line. If it looks fine, look at the line above it.
  3. Change one thing. Run again.
  4. No error but the wrong result? Say out loud what each line should do, and watch for the first one that does something else.
  5. Do not change three things at once; you will not know which one mattered.

Task: fix three errors

This program should turn the LED green, wait a second, drive 30 cm forward into the green zone and print arrived. It has one syntax error, one runtime error and one logic error. Fix them one at a time.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

led("green"
wait("1")
backward(50, distance=30)
print("arrived")

Challenges

  1. Write a program with a runtime error on its fifth line and nothing wrong before it. Check that the lines before it still run.
  2. Write a program with a logic error that makes the robot drive too far. Swap with a partner and find each other's.
  3. Try print(10 / 0). Which of the three kinds of error is it?

Past paper questions on this

Real exam questions that test this, each worked through with a program you can run.