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

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

提交回复
热议问题