npx skills add ...
npx skills add wshobson/agents --skill python-error-handling
Python error handling patterns including input validation, exception hierarchies, and partial failure handling. Use when implementing validation logic, designing exception strategies, handling batch processing failures, or building robust APIs.
npx skills add wshobson/agents --skill python-error-handling
Build robust Python applications with proper input validation, meaningful exceptions, and graceful failure handling. Good error handling makes debugging easier and systems more reliable.
Validate inputs early, before expensive operations. Report all validation errors at once when possible.
Use appropriate exception types with context. Messages should explain what failed, why, and how to fix it.
In batch operations, don't let one failure abort everything. Track successes and failures separately.
Chain exceptions to maintain the full error trail for debugging.
Validate all inputs at API boundaries before any processing begins.
Parse strings and external data into typed domain objects at system boundaries.
Use Pydantic models for structured input validation with automatic error messages.
Use Python's built-in exception types appropriately, adding context as needed.
| Failure Type | Exception | Example |
|---|---|---|
| Invalid input | ValueError | Bad parameter values |
| Wrong type | TypeError | Expected string, got int |
| Missing item | KeyError | Dict key not found |
| Operational failure | RuntimeError | Service unavailable |
| Timeout | TimeoutError | Operation took too long |
| File not found | FileNotFoundError | Path doesn't exist |
| Permission denied | PermissionError | Access forbidden |
Detailed sections (starting with ## Advanced Patterns) live in references/details.md. Read that file when the navigation summary above is insufficient.
ValueError, TypeError, not generic Exceptionraise ... from e to preserve debug info