How to prevent "too broad exception" in this case?

PythonExceptionPep8

Python Problem Overview


I have a list of functions that may fail and, if one fails, I don't want the script to stop, but to continue with next function.

I am executing it with something like this :

list_of_functions = [f_a, f_b, f_c]
for current_function in list_of_functions:
    try:
        current_function()
    except Exception:
        print(traceback.format_exc())

It's working fine, but it is not PEP8 compliant:

> When catching exceptions, mention specific exceptions whenever > possible instead of using a bare except: clause. > > For example, use: > > try: > import platform_specific_module > except ImportError: > platform_specific_module = None > > A bare except: clause will catch SystemExit and KeyboardInterrupt > exceptions, making it harder to interrupt a program with Control-C, > and can disguise other problems. If you want to catch all exceptions > that signal program errors, use except Exception: (bare except is > equivalent to except BaseException: ). > > A good rule of thumb is to limit use of bare 'except' clauses to two > cases: > > If the exception handler will be printing out or logging the traceback; at least the user will be aware that an error has occurred. > > If the code needs to do some cleanup work, but then lets the exception propagate upwards with raise . try...finally can be a better > way to handle this case.

How can I do this the good way?

Python Solutions


Solution 1 - Python

The PEP8 guide you quote suggests that it is okay to use a bare exception in your case provided you are logging the errors. I would think that you should cover as many exceptions as you can/know how to deal with and then log the rest and pass, e.g.

import logging

list_of_functions = [f_a,f_b,f_c]
for current_function in list_of_functions:
    try:
        current_function()
    except KnownException:
        raise
    except Exception as e:
        logging.exception(e)

Solution 2 - Python

Use this to cheat PEP8:

try:
    """code"""

except (Exception,): 
    pass

Solution 3 - Python

I think in some rare cases catching general exception is just justified and there is a way to trick PEP8 inspection:

list_of_functions = [f_a,f_b,f_c]
for current_function in list_of_functions:
try:
    current_function()
except (ValueError, Exception):
    print(traceback.format_exc())

You can replace ValueError by any other. It works for me (at least in PyCharm).

Solution 4 - Python

You can just put a comment like except Exception as error: # pylint: disable=broad-except that's worked for me actually. I hope it could be work for you.

Solution 5 - Python

From issue PY-9715 on yourtrack.jetbrains.com:

From pep-0348:

> BaseException > > The superclass that all exceptions must inherit from. It's name was > chosen to reflect that it is at the base of the exception hierarchy > while being an exception itself. "Raisable" was considered as a name, > it was passed on because its name did not properly reflect the fact > that it is an exception itself. > > Direct inheritance of BaseException is not expected, and will be > discouraged for the general case. Most user-defined exceptions should > inherit from Exception instead. This allows catching Exception to > continue to work in the common case of catching all exceptions that > should be caught. Direct inheritance of BaseException should only be > done in cases where an entirely new category of exception is desired. > > But, for cases where all exceptions should be caught blindly, except > BaseException will work.

Solution 6 - Python

You can avoid the error if you then re-raise the Exception. This way you are able to do damage control and not endanger loosing track of its occurance.

Solution 7 - Python

Do you perhaps mean that each function can raise different exceptions? When you name the exception type in the except clause it can be any name that refers to an exception, not just the class name.

eg.

def raise_value_error():
    raise ValueError

def raise_type_error():
    raise TypeError

def raise_index_error():
    doesnt_exist

func_and_exceptions = [(raise_value_error, ValueError), (raise_type_error, TypeError), 
    (raise_index_error, IndexError)]

for function, possible_exception in func_and_exceptions:
   try:
       function()
   except possible_exception as e:
       print("caught", repr(e), "when calling", function.__name__)

prints:

caught ValueError() when calling raise_value_error
caught TypeError() when calling raise_type_error
Traceback (most recent call last):
  File "run.py", line 14, in <module>
    function()
  File "run.py", line 8, in raise_index_error
    doesnt_exist
NameError: name 'doesnt_exist' is not defined

Of course that leaves you with not knowing what to do when each exception occurs. But since you just want to ignore it and carry on then that's not a problem.

Solution 8 - Python

First, generate the pylintrc using the below command

pylint --generate-rcfile > .pylintrc

For reference: https://docs.microsoft.com/en-us/visualstudio/python/linting-python-code?view=vs-2022

Search for disable (uncomment if needed) in the generate pylintrc file and add the below exception.

broad-except

Rerun the pylint command and see the magic

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBluskyView Question on Stackoverflow
Solution 1 - PythonEd SmithView Answer on Stackoverflow
Solution 2 - PythonAllex RaduView Answer on Stackoverflow
Solution 3 - PythonPiotr KarnasiewiczView Answer on Stackoverflow
Solution 4 - PythonHushenView Answer on Stackoverflow
Solution 5 - PythonxgqfrmsView Answer on Stackoverflow
Solution 6 - PythonJavoView Answer on Stackoverflow
Solution 7 - PythonDunesView Answer on Stackoverflow
Solution 8 - PythonSaurabhView Answer on Stackoverflow