没有说明符的期望与其他之间的区别?
Difference between expect without specifier vs else?
在 Python 的 try
、except
块中,如果我可以只使用不带说明符的 except:
,为什么 else
需要存在?
看来您对 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
在 Python 的 try
、except
块中,如果我可以只使用不带说明符的 except:
,为什么 else
需要存在?
看来您对 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