redline
HighError handlingbare-except

Bare except

Bare except. A bare except: also catches KeyboardInterrupt and SystemExit, and every bug in the block.

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

Flagged
try:
    load()
except:
    pass
Better
try:
    load()
except FileNotFoundError:
    return DEFAULTS

What 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.

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"
    }
  }
}

Read the source or report a false positive.