redline
LowError handlingraise-without-from

Re-raise loses the cause

Re-raise loses the cause. Raises a new error inside except without from. The original cause is hidden in the traceback.

This is how the comment reads inline, under the flagged line.

Why it matters

Raising a new exception inside except without from buries the original error in 'During handling…' noise.

What to do

raise NewError(...) from err (or from None when hiding it is the point).

Examples

Flagged
except KeyError:
    raise ConfigError('missing key')
Better
except KeyError as err:
    raise ConfigError('missing key') from err

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 KeyError:
    raise ValueError('x')

Left alone

try:
    go()
except KeyError as e:
    raise ValueError('x') from e

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": [
      "raise-without-from"
    ],
    "severity": {
      "raise-without-from": "medium"
    }
  }
}

Read the source or report a false positive.