Lesson 17 of 20 · 50 min · UG / professional
Exception classes and catching policy
except Exception swallows too much. except IndexError is precise. A campus error type (BadPin) lets callers catch your refusals without catching MemoryError.
Broken form input
Check values before you calculate.
You will be able to
- ✓ Catch the error you expect, not MemoryError by accident
- ✓ Give campus refusals their own type so callers can tell them apart
- ✓ Avoid except Exception around a whole program
Too much and too little
except Exception around a whole attendance job swallows bugs you did not mean to catch — including some you wanted to crash so a scheduler turns red. except IndexError is precise but will miss a KeyError on a weather JSON body. A campus type (BadPin) lets callers catch your refusals without catching MemoryError.
Hierarchies exist so CampusError can be the parent of BadPin and MissingFolder. Callers who care only about PIN catch BadPin. Ops who want any campus refusal catch CampusError. Nobody wraps the process in except Exception.
Named returns here; classes on a laptop
This sandbox has no raise. You return "Reject: pin" versus a generic "Error". The policy is the same: do not pretend every failure is the same string. Copy the class sample onto a laptop.
Laptop only — a small hierarchy; catch precisely
class CampusError(Exception):
pass
class BadPin(CampusError):
pass
def parse_pin(text):
if len(text) != 6:
raise BadPin("need 6 digits")
return text
try:
parse_pin("12")
except BadPin:
print("fix the PIN")
# except Exception: # too much — hides MemoryError
# except IndexError: # too little — misses this BadPin
Words that matter
- Exception class
- A named failure type callers can catch.
- Catch too much
- except Exception around a whole program hides bugs.
- Catch too little
- except IndexError when the real failure is KeyError or BadPin.
Common mistakes
Avoid: except Exception: around the entire nightly job.
Do this: Catch BadPin (or IndexError) where you can recover; let the rest crash.
Run it step by step
Each box is a real program. Press Run, change a number, Run again — the output must follow your code.
1. Step 1 — a named refusal, not a generic Error
Callers can tell PIN from folder.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
2. Step 2 — do not swallow everything
kind pin is ours. Anything else is not ours — do not pretend it is.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Example program — Two campus refusals
Separate strings now; separate classes on a laptop.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Your turn — Named pin refusal
If pin is "12", print Reject: pin so that text appears.
Edit the example, press Run, then Build if you want a compile check.
build
Press Build to compile.
Self-assessment
Check your understanding before you mark the lesson complete.
Progress is stored in a browser cookie on this device.