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

后端 未结 6 1952
情话喂你
情话喂你 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: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)
    

提交回复
热议问题