Python try except

后端 未结 5 1533
[愿得一人]
[愿得一人] 2021-01-19 00:12
try:
    #statement 1
    #statement 2
except Exception, err:
    print err
    pass

This may be very trivial but I never actually thought about it

5条回答
  •  南方客
    南方客 (楼主)
    2021-01-19 01:00

    1) Does statement 2 gets executed if an error is raised in statement 1?

    nope, statement 2 is not executed

    2) How does Exception deal with in a case where an error is raised for both statement 1 and statement 2? Which error does it print out in the above code? both?

    only statement 1 has a chance to raise an error, see above,

    NOTE: if you want statement 2 to execute always, you can use finally with the try/except

提交回复
热议问题