no-broad-catch
What it does
Section titled “What it does”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.
Why it matters
Section titled “Why it matters”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.
Example
Section titled “Example”A = B = 1try: passexcept: passexcept A: pass... passexcept exit.Exception: B = 2except (A, Exception): B = 2Configuration
Section titled “Configuration”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.
Upstream equivalents
Section titled “Upstream equivalents”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 |