Skip to content

python/fallback-on-exception

Pack Severity Fix Versions
Python warning none, edit by hand All versions

Catching a broad Exception without binding, logging, or re-raising it and calling a second implementation silently hides bugs behind a legacy fallback path. Fix the primary path or let the real error surface.

def compute_score(values: list[float]) -> float:
try:
return compute_score_v2(values)
except Exception:
return compute_score_v1(values)
return compute_score_v2(values)

The rule reports a try statement whose body runs a call (not an import) and whose bare except: or except Exception: clause binds no exception variable, does not log or raise, and returns or assigns the result of another call.

The rule leaves alone the cases in the list:

  • try/except ImportError blocks, which python/import-fallback handles separately.
  • Handlers that catch a specific expected error such as KeyError or ValueError.
  • Handlers that bind the exception (except Exception as error:) to inspect, report, log, or re-raise it.
  • Empty or pass handlers.

xpd has no automatic fix, so make the change by hand.

Remove the legacy fallback call and the broad except block. If a narrow, expected failure genuinely needs recovery, catch only that specific exception type and log or handle the error explicitly.

Catching a broad exception without logging or re-raising and calling a fallback implementation hides failures behind a legacy code path. Fix or fail loudly on the primary path.

See all rules in the Python pack.