How to continue with next line in a Python's try block?

前端 未结 5 1287
[愿得一人]
[愿得一人] 2021-01-05 09:30

e.g.

try:
    foo()
    bar()
except: 
    pass

When foo function raise an exception, how to skip to the next line (bar) and execute it?

5条回答
  •  一向
    一向 (楼主)
    2021-01-05 10:22

    That is not the intended way for try/except blocks to be used. If bar() should be executed even if foo() fails, you should put each in its own try/except block:

    try:
      foo()
    except:
      pass # or whatever
    
    try:
      bar()
    except:
      pass # or whatever
    

提交回复
热议问题