Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PEP8: Do not use bare except #264

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cclauss
Copy link
Contributor

@cclauss cclauss commented Nov 25, 2024

% ruff check --select=E722 --output-format=concise

lib/helpers.py:35:5: E722 Do not use bare `except`
lib/helpers.py:45:5: E722 Do not use bare `except`
lib/helpers.py:55:5: E722 Do not use bare `except`
loki.py:568:9: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:33:9: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:37:9: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:41:9: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:45:9: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:53:13: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:60:13: E722 Do not use bare `except`
plugins/loki-plugin-wmi.py:67:13: E722 Do not use bare `except`
Found 11 errors.

% ruff rule E722

bare-except (E722)

Derived from the pycodestyle linter.

What it does

Checks for bare except catches in try-except statements.

Why is this bad?

A bare except catches BaseException which includes
KeyboardInterrupt, SystemExit, Exception, and others. Catching
BaseException can make it hard to interrupt the program (e.g., with
Ctrl-C) and can disguise other problems.

Example

try:
    raise KeyboardInterrupt("You probably don't mean to break CTRL-C.")
except:
    print("But a bare `except` will ignore keyboard interrupts.")

Use instead:

try:
    do_something_that_might_break()
except MoreSpecificException as e:
    handle_error(e)

If you actually need to catch an unknown error, use Exception which will
catch regular program errors but not important system exceptions.

def run_a_function(some_other_fn):
    try:
        some_other_fn()
    except Exception as e:
        print(f"How exceptional! {e}")

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant