| Previous: Modules, imports, and packages | Notes index | Next: Files, paths, JSON, and CSV |
|---|
This chapter explains exception types, raising errors, handling failures, and cleanup.
- try, except, else, and finally
- Raising built-in and custom exceptions
- Exception chaining
- Context managers and cleanup
- Eight review questions
An exception interrupts normal execution and carries a type and message. Catch a specific exception when the program has a useful recovery action.
raw_count = "12"
try:
count = int(raw_count)
except ValueError:
count = 0
else:
print(f"Loaded {count} items")
print(count)The except block runs only when the matching exception occurs. The else block runs only when the try block finishes without an exception. Put narrower exception types before their broader parent types.
Do not use a bare except to hide programming errors. Catching BaseException also catches signals such as KeyboardInterrupt and should almost never be used for ordinary recovery.
Use a built-in exception type that describes the invalid operation. Define a custom exception when callers need to distinguish one domain failure:
class TaskNotFoundError(LookupError):
pass
def get_task(tasks, task_id):
try:
return tasks[task_id]
except KeyError as error:
raise TaskNotFoundError(f"Task {task_id} was not found") from errorraise ... from keeps the original exception as the cause. This gives a useful traceback without forcing a caller to understand the internal dictionary lookup.
Validate external inputs explicitly. assert is for developer assumptions and may be removed when Python runs with optimization, so it is not input validation.
finally runs whether the operation succeeds or raises. Prefer a context manager when a resource supports one, because it handles cleanup at the correct boundary:
from pathlib import Path
path = Path("tasks.txt")
with path.open("r", encoding="utf-8") as file:
contents = file.read()The file closes when the with block exits, including when reading raises an exception. Use context managers for files, locks, and other resources with a defined acquire/release lifecycle.
If cleanup itself can fail, decide how to preserve the original error. Do not silently discard either failure.
- When should a function catch an exception?
- What does the try block's else clause mean?
- Why should exception handlers catch specific types?
- Why is a bare except usually a poor choice?
- When should a custom exception be defined?
- What does raise NewError(...) from error preserve?
- Why is assert not appropriate for validating user input?
- How does a context manager help clean up a file?
| Previous: Modules, imports, and packages | Notes index | Next: Files, paths, JSON, and CSV |
|---|