The cash machine and the wrong PIN
You type the wrong PIN at a cash machine. It does not crash. It does not show a strange code. It says: "Wrong PIN. You have 2 tries left."
The machine expected a mistake. Good programs expect mistakes too. Good code is robust: it keeps working when something unexpected happens.
Key words
| Word | What it means | Example |
|---|---|---|
| Robust | Keeps working, or stops safely, when something unexpected happens. | |
| Exception | An error that Python raises while the program runs. | ValueError |
| try | Starts a block of code that might cause an exception. | |
| except | Starts the code that runs if the try block raises that exception. | |
| Unexpected input | Data the program did not plan for. | letters in an age box |
| Error message | Text that tells the user what went wrong and what to do. | |
| Validation | A check that data is sensible. | |
| Crash | The program stops because of an error it did not handle. |
The cells students fill in are left blank in this preview.
Run it: a crash
text = "twelve"
age = int(text)
print("Next year you will be", age + 1)
# Run it. Python raises an exception and the program stops.
# Read the last line of the message. What kind of exception is it?
Three signs of robust code
Robust code does three things.
- It copes with unexpected input. A letter in an age box does not crash it.
- It copes with unexpected endings. A missing file or a lost connection does not lose the user's work.
- It gives specific, meaningful error messages. The user learns what to do next.
Robust and not robust
| Situation | Not robust | Robust |
|---|---|---|
| The user types "abc" for an age | The program crashes | It says: please type a whole number |
| A data file is missing | A long error appears and the program stops | It says: budget.txt was not found. Check the name |
| The power fails while saving | The saved file is incomplete | The program saves often, so little is lost |
| The user divides by zero | The program stops | It says: you cannot divide by zero |
The cells students fill in are left blank in this preview.
Catching an exception
Python has a tool for this. Put the risky code in a try block. Put the plan B in an except block.
If nothing goes wrong, Python skips the except block. If an exception happens, Python jumps to it.
Try it: try and except
text = "twelve"
try:
age = int(text)
print("Next year you will be", age + 1)
except ValueError:
print("Please type your age as digits, for example 12.")
# Change "twelve" to "12" and run it again.
Try it: two more exceptions
try:
print(10 / 0)
except ZeroDivisionError:
print("You cannot divide by zero.")
try:
file = open("budget_2026.txt")
print(file.readline())
file.close()
except FileNotFoundError:
print("budget_2026.txt was not found. Check the file name.")
Name the exception
data-callouts.warning.label
Write except ValueError, not just except. A plain except hides every error, even your own bugs. Catch the exception you expect and let the rest show.
A good error message
| Bad message | Why it is bad | Better message |
|---|---|---|
| Error 7 | The user cannot tell what it means | Your age must be a whole number, such as 16. |
| Invalid input | It does not say which input or why | The postcode must have 5 to 8 characters. |
| Something went wrong | No clue what to do next | The file could not be saved. Check you have space, then try again. |
The cells students fill in are left blank in this preview.
Quick check: robust or not?
Have a go
Quick check: robust or not?
5 questionsYour answers are marked here and are not kept. A college account keeps every student’s marks.