Skip to content

no-broad-catch

PythonRubysuspiciousno fixdefault: warnpython/ruffruby/rubocop

Reports a catch, except or rescue clause that catches everything (a clause with no type, when the bare option is set) or one of the broad types, when it does not raise the error again and has no call that matches handled. Each language lists the broad types in its grammar’s rule row.

A broad clause also catches errors that the code did not expect, a typo that raises NameError or a user’s interrupt, and turns them into a silent wrong result. The error is gone, and nobody learns that the program is broken.

To fix, name the exceptions the code can handle, or raise the error again after the handling.

Reported
A = B = 1
try:
pass
except:
pass
except A:
pass
...
pass
except exit.Exception:
B = 2
except (A, Exception):
B = 2
python:
lint:
rules:
no-broad-catch:
level: warn # off, warn (default), error
options:
bare: true
types: []
handled: "^[A-Za-z_][A-Za-z0-9_.]*[.]exception[(]|exc_info[ ]*=[ ]*[^F ]"
ruby:
lint:
rules:
no-broad-catch:
level: warn # off, warn (default), error
options:
bare: true
types: []
handled: "^[A-Za-z_][A-Za-z0-9_.]*[.]exception[(]|exc_info[ ]*=[ ]*[^F ]"
Option Type Default Description
bare bool true A clause with no type is broad
types list [] Types that are broad
handled string `“^[A-Za-z_][A-Za-z0-9_.]*[.]exception[(] exc_info[ ]=[ ][^F ]“`

Enabled by the presets: python/ruff, ruby/rubocop.

Suppress one line with sudsy-disable-next-line no-broad-catch in a comment of the file’s language.

The same check in other tools; Sudsy reports what each upstream rule reports.

Tool Rule Match
rubocop Lint/RescueException full
ruff E722 full
ruff BLE001 full