Programming Fundamentals › Error Handling
Error Handling
Anticipating, detecting and responding to things going wrong.
Also known as: exception handling, handling errors
Error handling is anticipating that things will go wrong (bad input, missing files, network failures, bugs) and deciding what the program does about each.
Some errors you can handle, some you can only report:
| Situation | Reasonable response |
|---|---|
| User typed an invalid value | tell them what’s wrong, keep their input |
| A network call timed out | retry with backoff, then fail clearly (retry with backoff) |
| A file is missing | use a default, or stop with a clear message |
A bug (a None where it shouldn’t be) | fail loudly, log with details, fix the code |
try:
config = load_config(path)
except FileNotFoundError:
config = default_config() # a case you can recover from
Principles
- Handle errors where you know what to do. Low-level code often can’t decide, so it lets the error rise to code that can (exceptions).
- Catch specific errors, not everything (try/catch).
- Don’t ignore them. Swallowing hides bugs (swallowing errors).
- Fail fast when something’s wrong, instead of carrying bad data forward (fail fast).
- Clean up. Close files and connections in
finallyorwithblocks. - Give useful messages: what failed, with which input, and what to do next. Don’t leak secrets or internals to users.
- Log the details for developers, with the stack trace (logging).
- Think about the unhappy path early. What if the list is empty, the API returns 500, the disk is full?
- Make retries safe (idempotent), and degrade gracefully where possible (graceful degradation).