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:
print('Starting')
a = 10
b = 0
print(a / b)
print('Finished')Starting
Traceback (most recent call last):
File "divide.py", line 4, in <module>
print(a / b)
~~^~~
ZeroDivisionError: division by zeroLook 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.
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.
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:
| Error name | What you did | Fix |
|---|---|---|
| ZeroDivisionError | 10 / 0 | Check the divisor is not 0 before dividing |
| ValueError | int('twenty') | The text must look like a number: '20' |
| NameError | print(totl) | Spelling. The box is called total |
| TypeError | '10' + 5 | Cast 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.
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 zero4The 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:
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:
The error name and a plain-English message. This is what went wrong. Start here.
The line of your code that failed, copied out for you.
The file name and line number: File "sum.py", line 4.
6Run-time against syntax, side by side
| Syntax error | Run-time error | |
|---|---|---|
| Found | Before running | While running |
| Output before it | None at all | Everything up to that line |
| Caused by | Breaking a rule of the language | An impossible instruction or a bad value |
| Example | print('Hi' | print(10 / 0) |
7Recap
'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.A program prints 'Hello', then stops with ZeroDivisionError. Which kind of error is that?
The user types 'ten' into int(input()). What happens?
Which line of a traceback should you read first?