Engineering Python · Functions & Files
Errors and try/except
In Engineering Python because labs fail on bad casts and missing keys — reading tracebacks and catching expected errors keeps a notebook usable.
Errors interrupt a cell with a traceback. `try` / `except` recover from expected failures (bad user text, missing keys) so the rest of the lab can continue. Do not blanket-catch everything and hide bugs.
- Engineering Python
- Medium level
- 4 concepts
1Reading a traceback
The last lines name the exception type and message. The frames above show where the call came from. Fix the named line first.
Figure. The last lines name the exception type and message. The frames above show where the call came from. Fix the named line first.
Where should you look first in a traceback?
- The exception type and message at the end, then the named line
- Only the first blank line
- The operating system version
The exception message points at the failure.
2try / except for recovery
Wrap only the call you expect might fail. Catch a specific type (`ValueError`, `KeyError`) when you can.
Figure. raw is "x". int(raw) raises ValueError. The except ValueError branch sets n = 0 so the rest of the cell can print n. Wrap only the call you expect might fail.
Takeaway
- IdeaWrap only the call you expect might fail. Catch a specific type (`ValueError`, `KeyError`) when you can.
Catch ValueError
raw = "x"
try:
n = int(raw)
except ValueError:
n = 0
print(n)3Catch narrowly
Bare `except:` swallows KeyboardInterrupt and bugs. Prefer named exceptions and re-raise what you cannot handle.
Figure. Bare except: swallows KeyboardInterrupt and bugs. Prefer named exceptions and re-raise what you cannot handle.
Takeaway
- IdeaBare `except:` swallows KeyboardInterrupt and bugs. Prefer named exceptions and re-raise what you cannot handle.
4Lab: catch a deliberate error
Force a bad cast, catch ValueError, and print a fallback.
Figure. int('nope') is a deliberate bad cast. except ValueError sets n = 0 and print(n) shows the fallback 0.
Takeaway
- IdeaForce a bad cast, catch ValueError, and print a fallback.
Coding lab. Catch ValueError runs in the app, with checks on your output.
Notes
- In Engineering Python because labs fail on bad casts and missing keys — reading tracebacks and catching expected errors keeps a notebook usable.
- The last lines name the exception type and message. The frames above show where the call came from. Fix the named line first.
- Wrap only the call you expect might fail. Catch a specific type (`ValueError`, `KeyError`) when you can.
Exam traps & shortcuts
- Run one cell at a time and read stdout before changing more lines.
- Names are labels for values; rebinding a name does not rewrite old prints.
Recap
Tracebacks locate failures; try/except recovers from expected errors; catch narrowly.
- Reading a traceback
- The last lines name the exception type and message. The frames above show where the call came from. Fix the named line first.
- try / except for recovery
- Wrap only the call you expect might fail. Catch a specific type (`ValueError`, `KeyError`) when you can.
- Catch narrowly
- Bare `except:` swallows KeyboardInterrupt and bugs. Prefer named exceptions and re-raise what you cannot handle.
- Lab: catch a deliberate error
- Force a bad cast, catch ValueError, and print a fallback.
Practise Errors and try/except
Reading is free and needs no account. Practice, mocks and progress live in the app.
- A 2-question practice set that ends the chapter
- 1 quick check with worked explanations
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device