Bare except
This is how the comment reads inline, under the flagged line.
Why it matters
except: catches everything, including Ctrl-C and sys.exit, so the program can't even be stopped cleanly.
What to do
Catch the specific exceptions you expect.
Examples
try:
load()
except:
passtry:
load()
except FileNotFoundError:
return DEFAULTSWhat it catches
It reads the shape of the code: functions, blocks, loops and calls. Languages: Python. Scope: All files. These cases are the rule's own tests: the first set must be flagged and the second must not.
Flagged
try:
go()
except:
retry()
Left alone
try:
go()
except OSError:
retry()
Research
Bare `except:`, `except Exception`, and a re-raise that drops the cause. It catches bugs you did not expect along with the error you did, and loses the original traceback.
- Ruff: blind-except
- Ruff: bare-except
- skill: code-review
Turn it off or change it
In a config file for the CLI, or in the API's rules field:
{
"rules": {
"disable": [
"bare-except"
],
"severity": {
"bare-except": "low"
}
}
}
except:also catches KeyboardInterrupt and SystemExit, and every bug in the block.