没有说明者与其他人的期望之间的差异?

时间:2017-02-16 10:59:50

标签: python exception try-catch except

在Python的tryexcept块中,如果我可以在没有说明符的情况下使用else,为什么except:需要存在?

1 个答案:

答案 0 :(得分:2)

您似乎了解tryexceptelsefinally是如何关闭的。

通过查看https://docs.python.org/2/tutorial/errors.html,这里概述了它们如何协同工作:

try:
    #Try something that might raise an exception
except <exception specifier>:
    #Code here will only run if the exception that came up was the one specified
except:
    #Except clause without specifier will catch all exceptions
else:
    #Executed if try clause doesn't raise exception
    #You can only have this else here if you also have except blocks
finally:
    #Runs no matter what