LambdaLabTM
Computer Science · Class 11 · Errors
ErrorsRun-time⏱️ 8 min read

Run-time Errors

Every rule of the language is obeyed, so Python happily starts your program. Then, halfway through, it hits something it cannot do — and stops dead. That is a run-time error: a mistake that only shows up while the program is running.

1The program starts, then gives up

A syntax error is caught before the program starts. A run-time error is different: the writing is faultless, so Python begins, prints whatever comes first, and only then meets the impossible instruction.

Here is the classic one. Nothing is misspelt and no bracket is missing — but no number can be divided by zero:

divide.py
print('Starting')
a = 10
b = 0
print(a / b)
print('Finished')
Output
Starting
Traceback (most recent call last):
  File "divide.py", line 4, in <module>
    print(a / b)
          ~~^~~
ZeroDivisionError: division by zero

Look closely at that output. Starting was printed — those lines really did run. Finished was not, because the program never got there. The error cut the program in half.

Key Takeaway
A run-time error appears while the program is running. Whatever came before it has already happened; nothing after it runs. Python calls these exceptions.

2Watch it happen

Run this and see for yourself: the first two lines appear, then the program dies. Change b = 0 to b = 2 and run it again — the same program now finishes to the end.

halfway.py

3The four you will meet most

Run-time errors come with a name, and the name is the clue. These four cover almost everything that goes wrong in Class 11:

Read the name, find the mistake
Error nameWhat you didFix
ZeroDivisionError10 / 0Check the divisor is not 0 before dividing
ValueErrorint('twenty')The text must look like a number: '20'
NameErrorprint(totl)Spelling. The box is called total
TypeError'10' + 5Cast first: int('10') + 5

The last two are old friends. NameError is Python saying “I have no box with that label” — nearly always a typo, or using a variable before you made it. TypeError is the wall you met in the casting lessons: text and numbers do not mix.

four_errors.py
print(totl)         # NameError: name 'totl' is not defined
print('10' + 5)     # TypeError: can only concatenate str (not "int") to str
print(int('twenty'))# ValueError: invalid literal for int() with base 10: 'twenty'
print(10 / 0)       # ZeroDivisionError: division by zero

4The sneaky part: the user causes them

A run-time error can hide in a program that worked perfectly yesterday, because it depends on what the user types. This program is correct for one person and broken for the next:

user_age.py

Run it and type 15 — it works. Run it again and type fifteen — the same program, the same code, and now a ValueError. Nothing about the program changed. That is why run-time errors slip past testing: you tried it with a sensible value, and your user did not.

5Reading a traceback

The block Python prints when it gives up is called a traceback. It looks frightening and says only three things. Read it from the bottom up:

1
The last line

The error name and a plain-English message. This is what went wrong. Start here.

2
The line above it

The line of your code that failed, copied out for you.

3
The line above that

The file name and line number: File "sum.py", line 4.

Bottom line first
Beginners read a traceback from the top and get lost. The useful sentence is always the last one. Read it, then look up one line to see which of your lines caused it.

6Run-time against syntax, side by side

Syntax errorRun-time error
FoundBefore runningWhile running
Output before itNone at allEverything up to that line
Caused byBreaking a rule of the languageAn impossible instruction or a bad value
Exampleprint('Hi'print(10 / 0)

7Recap

Key Takeaway
A run-time error (an exception) happens while the program is running: the code is written correctly, but it asks for something impossible — dividing by zero, turning 'twenty' into a number, using a name that does not exist, adding text to a number. Lines before it have already run; lines after it never will. The traceback names the error and the line, and is read from the bottom up.
Quick Check

A program prints 'Hello', then stops with ZeroDivisionError. Which kind of error is that?

Quick Check

The user types 'ten' into int(input()). What happens?

Quick Check

Which line of a traceback should you read first?