Python Ignore Exception and Go Back to Where I Was

后端 未结 7 1401
孤街浪徒
孤街浪徒 2021-02-13 12:47

I know using below code to ignore a certain exception, but how to let the code go back to where it got exception and keep executing? Say if the exception \'Exception\' raises in

7条回答
  •  逝去的感伤
    2021-02-13 13:25

    There's no direct way for the code to go back inside the try-except block. If, however, you're looking at trying to execute these different independant actions and keep executing when one fails (without copy/pasting the try/except block), you're going to have to write something like this:

    actions = (
        do_something1, do_something2, #...
        )
    for action in actions:
        try:
            action()
        except Exception, error:
            pass
    

提交回复
热议问题