Recipes: Error Handling¶
Short, task-focused how-tos for specific things you'll want to do with exceptions. Each recipe assumes you already know the basics — if a term surprises you, the Learn section is one click away.
Unlike the tutorials, the recipes don't have a recommended order. Pick whichever is useful right now.
Recipes in this guide¶
- Create custom exceptions — building a small exception hierarchy for your project, including naming conventions and useful attributes.
- Handle multiple exceptions — handling several exception types in one block, re-raising, and exception chaining.
- Use context managers — the
withstatement, file and connection cleanup, and writing your own__enter__/__exit__pairs. - Avoid common error handling mistakes — bare
except, swallowed exceptions, and other anti-patterns that hide bugs.