【问题标题】:Difference between expect without specifier vs else?没有说明符的期望与其他之间的区别?
【发布时间】:2017-07-05 10:45:52
【问题描述】:
在 Python 的 try、except 块中,如果我可以使用不带说明符的 except:,为什么还要存在 else?
【问题讨论】:
标签:
python
exception
try-catch
except
【解决方案1】:
您对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