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

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

    Can't be done if the call to bar is inside the try-block. Either you have to put the call outside of the try-except block, or use the else:

    try:
        foo()
    except:
        pass
    else:
        bar()
    

    If bar might throw an exception as well, you have to use a separate try block for bar.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题