Empty Except Block

ID

python.empty_except_block

Severity

high

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Python

Tags

exception-handling, reliability

Description

Reports except clauses whose body is empty or just pass. An empty handler swallows the exception silently and converts every error in the protected block into a "no problem" — the kind of bug that surfaces only as unexplained empty results in production.

try:
    work()
except:
    pass               # FLAW

try:
    work()
except ValueError:
    pass               # FLAW

try:
    work()
except ValueError as e:
    log.error(e)       # OK — handler does something

Rationale

Silent failure compounds: the caller sees a successful return, builds further state on top of the assumed-good result, and the actual error trail starts dozens of frames away from the cause. Even a single log.warning(…​) at the catch point preserves enough information to debug later.

Remediation

At minimum, log the exception. If the catch is intentional (e.g. a best-effort cache cleanup), make that explicit:

import logging
log = logging.getLogger(__name__)

try:
    cache.delete(key)
except FileNotFoundError:
    log.debug("cache entry %s already gone", key)