Generic Exception Handling in Python the “Right Way”

前端 未结 6 1173
夕颜
夕颜 2020-12-23 00:07

Sometimes I find myself in the situation where I want to execute several sequential commands like such:

try:
    foo(a, b)
except Exception, e:
    baz(e)
tr         


        
6条回答
  •  梦毁少年i
    2020-12-23 00:38

    In your specific case, you can do this:

    try:
        foo(a, b)
        bar(c, d)
    except Exception, e:
        baz(e)
    

    Or, you can catch the exception one step above:

    try:
        foo_bar() # This function can throw at several places
    except Exception, e:
        baz(e)
    

提交回复
热议问题