在Python的try
,except
块中,如果我可以在没有说明符的情况下使用else
,为什么except:
需要存在?
答案 0 :(得分:2)
您似乎了解try
,except
,else
和finally
是如何关闭的。
通过查看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