Phase 1: Prepare
Step 1 Warm-up recall
From M7.L1: which error does int("abc") cause?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- write try and except for a named error (Foundation);
- choose a safe response (Core);
- avoid hiding faults (Core).
Phase 2: Teach
Step 3 A familiar example
A cashier keys a price. If the entry is not a number, she asks again rather than closing the till. try and except lets a program do the same.
Step 4 Clear explanation
Some failures are expected: a user types text where a number is needed, or a file is missing. try runs the risky step; except says what to do if a named error occurs.
text = "abc"
try:
qty = int(text)
print("Quantity", qty)
except ValueError:
print("Please enter a whole number")
print("Program continues")Please enter a whole number
Program continuesAlways name the error you expect. A bare except: catches every error, including your own mistakes, and hides them. That turns a program that stops loudly into one that quietly gives wrong answers.
A good response does one of four things: ask again, use a safe default, skip and report, or stop with a clear message. Do not simply say nothing. In Module 6 a bad row was counted and reported; here the same idea is applied through an exception.
Step 5 Worked example (4 marks)
Predict the output of try: n = int("7"); print(10 / n) except ZeroDivisionError: print("zero"), then of the same program with "0" in place of "7".
- With "7": n is 7 and 10 / 7 is printed as about 1.43. (1 mark)
- With "0": int works and n is 0. (1 mark)
- 10 / 0 raises ZeroDivisionError, which is caught, so zero is printed. (1 mark)
- Answer: 1.428..., then zero. (1 mark)
Go deeper: the professor's notes
Name the error.
Ask or report.
Hide it.
Responses
| Response | Good? |
|---|---|
| report and continue | yes |
| say nothing | no |
At work: A bad entry
A user types text for a quantity.
- Catch ValueError.
- Ask again.
What a careful practitioner decides. Handle expected failures; do not hide mistakes.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. Code that might fail goes in:
try runs the risky step.
2. except ValueError catches:
Name what you expect.
3. A bare except: is risky because it:
It catches everything.
4. A good response to an expected error is to:
Never silent.
Phase 4: Close
Step 7 Recap
- try runs the risky step; except handles a named error.
- Name the error; never use a bare except.
- Respond clearly: ask again, default, report or stop.
Step 8 Exit ticket (ungraded)
Write try and except so that a text entry such as "x" for a quantity prints a message instead of stopping.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a loop that keeps asking for a whole number until the user gives one.
In industry: Therac-25: reused software, unseen link
The quality assurance manager at the manufacturer was apparently unaware that some Therac-20 routines were also used in the Therac-25. A bug linked to one of the accidents was later found in the Therac-20 software.
Link to this lesson. Reused code carries its old faults with it; know where each piece of your program came from and test it again in the new setting.
Source: Leveson and Turner, IEEE Computer (July 1993)
Common mistakes
| Mistake | Why it fails |
|---|---|
| A bare except | Name the error you expect. |
| Handling an error and saying nothing | Report it or count it. |
| Putting too much code inside try | Keep the risky step small. |
Limits. This lesson does not cover else, finally or raising your own errors, which are useful in larger programs.