python catch exception and continue try block

前端 未结 9 2432
眼角桃花
眼角桃花 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:03

    Depending on where and how often you need to do this, you could also write a function that does it for you:

    def live_dangerously(fn, *args, **kw):
        try:
            return fn(*args, **kw)
        except Exception:
            pass
    
    live_dangerously(do_smth1)
    live_dangerously(do_smth2)
    

    But as other answers have noted, having a null except is generally a sign something else is wrong with your code.

提交回复
热议问题