Python Ignore Exception and Go Back to Where I Was

后端 未结 7 1397
孤街浪徒
孤街浪徒 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:17

    you could have all of the do_something's in a list, and iterate through them like this, so it's no so wordy. You can use lambda functions instead if you require arguments for the working functions

    work = [lambda: dosomething1(args), dosomething2, lambda: dosomething3(*kw, **kwargs)]
    
    for each in work:
        try:
            each()
        except:
           pass
    
    cleanup()
    

提交回复
热议问题