python-error-handling
/python-error-handling
Design exception hierarchies, catch narrowly, and preserve error context so failures stay diagnosable. Use when structuring errors in a Python library or application, or cleaning up broad except blocks.
- Author
- Amey-Thakur ↗
- Repository
- AI-SKILLS ↗
- Licence
- MIT
- Last repository change
- 05.09.2026
Assessment and security
- Files in this package
- 1
- Scripts
- 0
- HTML comments
- 0
- URLs in SKILL.md
- 0
No signals detected. Repository changed on 05.09.2026, without references to retired models or APIs.
MIT, open: use and modify while retaining author attribution.
What it does and when to use it
Design exception hierarchies, catch narrowly, and preserve error context so failures stay diagnosable. Use when structuring errors in a Python library or application, or cleaning up broad except blocks.
Original author description
Design exception hierarchies, catch narrowly, and preserve error context so failures stay diagnosable. Use when structuring errors in a Python library or application, or cleaning up broad except blocks.
How it works
- Method
- Boundaries
SKILL.md preview
---
name: python-error-handling
description: Design exception hierarchies, catch narrowly, and preserve error context so failures stay diagnosable. Use when structuring errors in a Python library or application, or cleaning up broad except blocks.
---
# Python error handling
An exception is part of your API. Callers can only handle what you raise
deliberately and document; everything else is a bug report.
## Method
1. **Define one root exception per package.** `class AppError(Exception)`,
then a small tree of specific subclasses (`ConfigError`, `RetryableError`).
Callers catch your root to shield themselves from your internals, or a
leaf to handle one case. Never raise bare `Exception`.
2. **Catch the narrowest type that you can act on.** `except OSError` when
you handle missing files; never `except Exception` except at a top-level
boundary that logs and exits or returns a 500. An unhandled exception with
a clean traceback beats a swallowed one every time.
3. **Preserve context when translating.** Re-raise with
`raise ConfigError("bad port") from exc` so the original traceback chains.
`from None` is only for deliberately hiding an implementation detail, and
deserves a comment.
4. **Keep try blocks one line where possible.** Wrap only the statement that
can fail, not the whole function; a broad block hides which call raised
and catches errors you never meant to.
5. **Put cleanup in finally or a context manager.** Resource release must not
depend on success. If you write the same try/finally twice, write a
`contextlib.contextmanager` instead.
6. **Use ExceptionGroup for concurrent failures.** On 3.11+, TaskGroup and
your own fan-out code raise groups; handle them with `except*` per type
rather than flattening to the first error and losing the rest.
7. **Decide retryable vs fatal at raise time.** Encode it in the type
(`RetryableError`) or an attribute, not by string-matching messages at the
call site.
## Boundaries
- Exceptions are for exceptional paths. An expected absent value is a `None`
return or a sentinel, not a raise; parsing user input that often fails may
deserve a result object instead.
- Do not log and re-raise at every level; one log at the boundary where the
exception is finally handled, or you flood logs with duplicates.
- Never use exceptions for flow control across module boundaries; that is an
API contract disguised as an accident.
Versions
| Repository | Score | Licence |
|---|---|---|
| This page · Amey-Thakur/AI-SKILLS | 90 | MIT |
| wshobson/agents | 100 | MIT |
| julianobarbosa/claude-code-skills | 100 | MIT |
| wesleyegberto/software-engineering-skills | 100 | MIT |
Database snapshot: 13.11.2026 · package version 2480ef14cd03 (SHA-256 digest of the entire package)
Downloads in the last 30 days: 0 · ranking
Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.
Similar skills
- 100php-error-handling
Implement modern PHP error and exception handling patterns. Use when implementing exception hierarchies, error handlers, or custom exceptions in PHP. - 98error-handling-patterns
Design error handling strategies for TypeScript and Python applications — exception hierarchies, Result/Either types, retry patterns, error boundaries, and structured error logging. Use when designing error handling architecture, choosing between exceptions and Result types,… - 100handling-errors
Prevents silent failures and context loss in error handling. Use when writing try-catch blocks, designing error propagation, reviewing catch blocks, or implementing Result patterns. - 100error-handling
Consistent error handling across frontend and backend layers. TRIGGER when: implementing error boundaries, exception handlers, or try/catch blocks. SKIP: shaping HTTP error response bodies (use api-design); logging the error (use logging-standards). (Examples in React + Python.) - 90error-handling-patterns
Design and review error handling in a codebase: which errors are expected results versus bugs, typed error hierarchies or result types, where to catch (boundaries) and where never to, preserving cause chains, mapping errors to HTTP and CLI exit codes, retries with backoff for… - 100swift-error-handling
Standards for throwing functions, Result type, and Never. Use when implementing Swift error throwing, designing error hierarchies, using Result types, or adding do-catch blocks.