Skip to content

no-throw-generic

Pythonstyleno fixdefault: offpython/ruff

Reports a raise or throw of one of the generic types, itself or a call of it. Each language lists the types in its grammar’s rule row.

A caller cannot tell a generic exception from any other error, so it can only catch everything or nothing, and the generic type also catches errors from other code. A specific type lets a caller handle the case.

To fix, raise a specific exception: a built-in one that fits (ValueError), or a class of your own.

Reported
...
pass
raise Exception("x")
...
python:
lint:
rules:
no-throw-generic:
level: warn # off (default), warn, error
options:
types: ["Exception"]
Option Type Default Description
types list ["Exception"] Generic exception types

Enabled by the presets: python/ruff.

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

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

Tool Rule Match
ruff TRY002 full