Skip to main content

A flake8 extension that checks for blind, catch-all except: and except Exception: statements.

As of pycodestyle 2.1.0, “E722 do not use bare except, specify exception instead” is built-in. However, bare Exception and BaseException are still allowed. This extension flags them as B902.

Using except without explicitly specifying which exceptions to catch is generally considered bad practice, since it catches system signals like SIGINT. You probably want to handle system interrupts differently than exceptions occurring in your code.

It’s also usually better style to have many small try-except blocks catching specific exceptions instead of a giant try: block with a catch-all except: at the bottom. It’s also nicer to your fellow programmers to be a bit more specific about what exceptions they can expect in specific parts of the code, and what the proper course of action is when they occur.

An example of code that will fail this check is:

try:
    something_scary()
except:
    everybody_panic()

However, the following code is valid:

try:
    something_terrifying()
except TerrifyingException:
    dont_panic()

Installation

If you don’t already have it, install flake8:

$ pip install flake8

Then, install the extension:

$ pip install flake8-blind-except

Usage

Run the following to verify that the plugin has been installed correctly:

$ flake8 --version
2.0 (pep8: 1.4.6, flake8-blind-except: 0.1.0, pyflakes: 0.7.3)

Now, when you run flake8, the plugin will automatically be used.

When a blind except is found, flake8 will output:

B901 blind except: statement

or:

B902 blind except Exception: statement

Contributing

I’m not working on Python these days, so probably won’t be making updates anytime soon. PRs are welcome though!

Testing

Tests can be run with pytest --doctest-modules flake8_blind_except.py.

Changes

0.2.1 - 2022-03-08

  • Remove setuptools from install_requires (#8)

0.2.0 - 2021-01-07

  • B902 error added for cases where a blind Exception is caught.

0.1.1 - 2016-06-27

  • pep8 was renamed to pycodestyle in its 2.0 release. Compatibility update for this change.

0.1.0 - 2014-02-07

  • Initial release

Notes

I’ve tested this package with flake8 2.6.2 + Python 2.7.3 and flake8 3.7.9 + Python 3.7.5.

Release files for flake8-blind-except 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flake8-blind-except 0.2.1
File Size Uploaded
flake8-blind-except-0.2.1.tar.gz 3.7 kB Details

Release files / flake8-blind-except-0.2.1.tar.gz

Download URL flake8-blind-except-0.2.1.tar.gz
Size 3.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f25a575a9dcb3eeb3c760bf9c22db60b8b5a23120224ed1faa9a43f75dd7dd16
BLAKE2b-256 checksum
How to use checksums
bef8cb5e0b8c948cb347b8942cb7ce73292a77ec8f9eafb8d5db999c5c1ac62f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

Release history Release notifications | RSS feed

This release

0.2.1 This release

1 release file

0.2.0

1 release file

0.1.1

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page