On Error Resume Next in Python

后端 未结 7 1175
栀梦
栀梦 2020-12-11 02:07

Snippet 1

do_magic() # Throws exception, doesn\'t execute do_foo and do_bar
do_foo()
do_bar()

Snippet 2

try:
    do_mag         


        
7条回答
  •  长情又很酷
    2020-12-11 02:35

    you could try a nested ´try´ loop, alltho that might not be as elegantly pythonic as you might want. the ´lambda´ solution is is also a good way to go, did not mention because it was done in the previous answer

    edit:

    try:
        do_magic()
    finally:
        try:
            do_foo()
        finally:
            try:
                do_bar()
            except:
                pass
    

    edit 2:

    well damnnit, this answer just got posted seconds beforehand again :|

提交回复
热议问题