python catch exception and continue try block

前端 未结 9 2451
眼角桃花
眼角桃花 2020-11-29 00:59

Can I return to executing try-block after exception occurs? (The goal is to write less) For Example:

try:
    do_smth1()
except:
    pass

try:
    do_smth2(         


        
9条回答
  •  一个人的身影
    2020-11-29 01:17

    While the other answers and the accepted one are correct and should be followed in real code, just for completeness and humor, you can try the fuckitpy ( https://github.com/ajalt/fuckitpy ) module.

    Your code can be changed to the following:

    @fuckitpy
    def myfunc():
        do_smth1()
        do_smth2()
    

    Then calling myfunc() would call do_smth2() even if there is an exception in do_smth1())

    Note: Please do not try it in any real code, it is blasphemy

提交回复
热议问题