How to prevent “too broad exception” in this case?

后端 未结 6 1959
情话喂你
情话喂你 2021-02-05 00:00

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 th

相关标签:
6条回答
  • 2021-02-05 00:40

    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 讨论(0)
  • 2021-02-05 00:41

    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 讨论(0)
  • 2021-02-05 00:48

    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.

    0 讨论(0)
  • 2021-02-05 00:51

    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.

    0 讨论(0)
  • 2021-02-05 00:56

    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)
    
    0 讨论(0)
  • 2021-02-05 00:59

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

    0 讨论(0)
提交回复
热议问题