python-try-except

Audits Python try/except blocks for overly broad scope and inappropriate exception catches.

1|2|Updated Nov 25, 2017
One-click install
npx skills add https://github.com/asarchami/dotfiles --skill python-try-except-asarchami
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: python-try-except
Source: https://github.com/asarchami/dotfiles/tree/main/dot_config/opencode/skills/python/try-except
Command: npx skills add https://github.com/asarchami/dotfiles --skill python-try-except-asarchami

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Python codebases often accumulate try/except blocks that are too broad, catch the wrong exceptions, or silently swallow errors, making bugs hard to detect and debug. This Skill systematically reviews every try/except block and tightens exception handling so failures surface correctly. ## Core Features & Use Cases - Scope Analysis: Checks whether each try block contains only the operation that can actually raise the expected exception, moving setup and processing code out. - Exception Appropriateness Check: Flags cases where catching KeyError, AttributeError, or IndexError should be replaced with in, .get(), hasattr(), or length checks. - Handler Risk Ranking: Ranks except clauses from most to least dangerous (bare except, except Exception, tuples, specific types) and identifies handlers that mask failures via pass or default returns. - Use Case: Before merging a large Python pull request, run this audit to find a bare except: that was silently hiding database connection failures in production. ## Quick Start Ask the AI to audit all try/except blocks in your Python project and tighten each one so the try covers only the operation that can raise the expected exception.

Frequently Asked Questions about python-try-except

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I find overly broad except clauses in Python code?

Grep for `try:` across your files, then rank each except clause from most to least dangerous: bare `except:`, then `except Exception:`, then tuples, then specific types. Each caught type should have a clear justification tied to what the try block can actually raise.

When should I use if statements instead of try/except in Python?

Use conditional checks for local values you control: `if key in d` or `d.get(key)` instead of catching KeyError, `hasattr(x, "y")` instead of catching AttributeError, and length checks instead of IndexError. Reserve try/except for external state like filesystem, network, and parsing operations.

How do I narrow the scope of a Python try block?

Move setup statements above the try and processing logic after it or into an `else` clause, so the try contains only the single operation that can raise the expected exception. Nested try/except blocks often signal the outer block is too wide.

Why is catching KeyError on dict access considered an anti-pattern?

Catching KeyError on `d[key]` hides whether the key was missing and can mask typos or logic errors. Using `d.get(key)` or `if key in d:` makes the missing-key case explicit and keeps exception handling reserved for genuinely exceptional conditions.

What are the risks of narrowing exception handling in existing code?

Narrowing a broad except clause can surface errors that were previously silenced, changing control flow and potentially breaking callers that relied on swallowed failures. Always trace callees to know what they can raise and run the test suite after editing.