How it works
Exception handling separates the expected path from the recovery path. Catching everything hides the bugs you needed to see.
Catch the specific exception you can actually recover from, and let the rest surface.
Common error
A bare catch-all that swallows genuine errors and turns a crash into wrong output.
Worked example
Python
values = ["3", "bad", "5"]
total = 0
for value in values:
try:
total += int(value)
except ValueError:
print("skipped", value)
print(total)Output
skipped bad
8Catch the specific failure expected at the conversion boundary. The program should expose skipped work or another defined outcome rather than silently hiding every possible error.
Exercises
5.4.1 · Explain
Why is an unrestricted except block risky in an automation script?
Show solution
It can hide unrelated programming mistakes and make failed work look successful. Catch expected exceptions and report or propagate other failures.
5.4.2 · Write code
Convert ["2", "x", "4"] into integers, skipping invalid entries, and print the result.
Show solution
Handle ValueError at the individual conversion.
One valid solution
result = []
for value in ["2", "x", "4"]:
try:
result.append(int(value))
except ValueError:
continue
print(result)Expected output
[2, 4]