101

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?

5
  • I don't understand what you're asking. What kinds of exceptions do you want to handle?
    – BrenBarn
    Commented May 25, 2015 at 16:16
  • 1
    what exceptions might be raised by your function? Commented May 25, 2015 at 16:18
  • 4
    except Exception: is different than bare except:. That PEP8 section warns about the latter, you are doing the former.
    – roippi
    Commented May 25, 2015 at 16:19
  • 1
    @haifzhan Any exception can be raised, I don't control the code behind functions
    – Blusky
    Commented May 25, 2015 at 16:25
  • 4
    @roippi No, it shows the same complaint about except Exception:.
    – Dr_Zaszuś
    Commented May 18, 2020 at 15:35

8 Answers 8

84

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)
4
  • 1
    except KeyboardInterrupt: raise is strictly unnecessary here. KeyboardInterrupt will never be caught by except Exception because KeyboardInterrupt derives from BaseException, not Exception.
    – roippi
    Commented May 25, 2015 at 20:35
  • 1
    Just wanted an example of a known exception, didn't realise KeyboardInterrupt is a bad example. I've changed it to some generic KnownException...
    – Ed Smith
    Commented May 25, 2015 at 20:38
  • 1
    A bare except is more suitable here. logging.exception() will automatically record the error and traceback.
    – lovetl2002
    Commented Oct 16, 2017 at 12:44
  • 5
    Hey, I am doing this in a couple places and yet, pylint throws too broad an exception.. Commented Oct 30, 2019 at 20:47
60

Use this to cheat PEP8:

try:
    """code"""

except (Exception,): 
    pass
6
  • 6
    cleanest solution... one extra char only, nothing unnecessary or non-truthinessedly added. I think this check is crazy... even if you predict all possible exceptions, you should always have the catchall at the end for things not predicted... it is pervasively recommend.
    – gunslingor
    Commented Jun 29, 2021 at 12:43
  • 1
    What does this do?
    – Yair V.
    Commented Oct 14, 2021 at 7:36
  • 3
    @Yair It provides a catch-all exception for all the possible errors that could occur, without any complaints from your IDE.
    – Allex Radu
    Commented Oct 15, 2021 at 22:59
  • 2
    How to log the exception in this case? except (Exception as e, ): seems not allowd in braces
    – sjd
    Commented Jan 25, 2022 at 10:45
  • 6
    except (Exception,) as e:
    – Allex Radu
    Commented Jan 29, 2022 at 8:39
19

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).

18

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.

2
  • 6
    Actually, this comment is a concise way of saying: "I'm aware that broad exceptions should be avoided, but in this case I want it and I know what I'm doing." --- You can set your workflow to search for such directive, if you think people abuse them. Commented Apr 8, 2021 at 18:01
  • 1
    Should be the accepted answer, so clean !
    – CBrunain
    Commented Mar 15, 2023 at 16:05
3

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

"Too broad exception clauses" inspection

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.

1

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.

0

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.

0

First, generate the pylintrc using the below command

pylint --generate-rcfile > .pylintrc

For reference: https://learn.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

Not the answer you're looking for? Browse other questions tagged or ask your own question.