没有说明符的expect与else之间的区别?

2024-04-27 04:51:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在Python的tryexcept块中,如果我可以只使用except:而不使用说明符,为什么else需要存在?你知道吗


Tags: elsetryexcept说明符
1条回答
网友
1楼 · 发布于 2024-04-27 04:51:14

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

相关问题 更多 >