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

前端 未结 5 1256
[愿得一人]
[愿得一人] 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:24

    Take bar() out of the try block:

    try:
        foo()
    except: 
        pass
    bar()
    

    Btw., watch out with catch-all except clauses. Prefer to selectively catch the exceptions that you know you can handle/ignore.

提交回复
热议问题