# Python Error Handling

> Design Python error paths with narrow exceptions, rollback, contextual logs, and preserved blocker truth. Use when handling retries, verifier outcomes, parser failures, or exception flow in Python services.

- Skill: `hoangnguyen0403/python-error-handling` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add hoangnguyen0403/python-error-handling`
- Raw SKILL.md: https://api.skillmd.com/api/skills/hoangnguyen0403/python-error-handling/raw
- Safety review: pending (external: skill-scanner PASS, skillspector PASS)
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: HoangNguyen0403 (https://skillmd.com/u/hoangnguyen0403)
- Updated: 2026-09-09
- Page: https://skillmd.com/skills/hoangnguyen0403/python-error-handling

---


# Python Error Handling

## **Priority: P1 (HIGH)**

## Rules

- Catch the narrowest exception you can explain.
- Roll back mutable state before re-raising or returning failure status.
- Add context to logs and raised errors without discarding the root cause.
- Preserve blocker truth; do not convert real failure into vague success text.

## Recipe

1. **Handle expected failures close to the boundary**.
2. **Re-raise unexpected failures** after cleanup.
3. **Return structured blocker or verdict data** when the caller needs workflow truth.
4. **Test timeout, malformed input, and missing-env paths**.

## Anti-Patterns

- **No silent `except Exception: pass`**.
- **No broad catch that marks work complete** without evidence.
- **No duplicate logging at every stack layer**.
- **No blocker text without owner or next action** when the protocol needs both.

## References

- [Framework Map](../references/framework-map.md)

