SCR106: bare-except
except: without an exception type
| Code | SCR106 |
| Name | bare-except |
| Category | Scripts (SCR) |
| Default severity | warning |
| Enabled by default | yes |
| Fix available | no |
| ign-lint equivalent | none |
What it does
Section titled “What it does”Reports except: clauses without an exception type, unless the handler
re-raises (a bare raise directly in its body).
Why is this bad?
Section titled “Why is this bad?”A bare except: catches everything: SystemExit, KeyboardInterrupt,
GeneratorExit and, in Jython, Java exceptions too. It hides programming
errors such as a misspelled name (NameError) along with the failure it
was meant for.
Note that in Jython except Exception: does not catch Java exceptions
(java.lang.NullPointerException, SQL exceptions from system.db.*).
Name both when that is the intent:
from java.lang import Throwable
try: system.db.runPrepUpdate(query, args)except (Exception, Throwable), e: logger.error("update failed: %s" % e)Example
Section titled “Example”try: value = float(payload["value"])except: value = 0.0Use instead:
try: value = float(payload["value"])except (KeyError, TypeError, ValueError): value = 0.0pylint
Section titled “pylint”Corresponds to W0702 bare-except (pylint also reports handlers that
re-raise).
Generated from the rule metadata by iglint generate-docs; iglint rule SCR106 prints the same page in the terminal. Source: docs/rules/SCR106.md.