【问题标题】:Difference between expect without specifier vs else?没有说明符的期望与其他之间的区别?
【发布时间】:2017-07-05 10:45:52
【问题描述】:

在 Python 的 tryexcept 块中,如果我可以使用不带说明符的 except:,为什么还要存在 else

【问题讨论】:

标签: python exception try-catch except


【解决方案1】:

您对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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-22
    • 2014-02-23
    • 2014-01-06
    • 2012-10-01
    • 2011-04-11
    • 2013-11-26
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多