Python 没有说明符的expect与else之间的差异?
在Python的Python 没有说明符的expect与else之间的差异?,python,exception,try-catch,except,Python,Exception,Try Catch,Except,在Python的try,except块中,如果我可以只使用except:而不使用说明符,那么为什么else需要存在呢?似乎你对try,except,else,以及最后的理解是关闭的 以下是从以下几个方面总结它们是如何协同工作的: 试试看: #尝试可能引发异常的操作 除: #仅当出现的异常是指定的异常时,此处的代码才会运行 除: #不带说明符的Except子句将捕获所有异常 其他: #如果try子句未引发异常,则执行 #如果还有except块,则只能在此处使用此else 最后: #不管发生什么事都
try
,except
块中,如果我可以只使用except:
而不使用说明符,那么为什么else
需要存在呢?似乎你对try
,except
,else
,以及最后的理解是关闭的
以下是从以下几个方面总结它们是如何协同工作的:
试试看:
#尝试可能引发异常的操作
除:
#仅当出现的异常是指定的异常时,此处的代码才会运行
除:
#不带说明符的Except子句将捕获所有异常
其他:
#如果try子句未引发异常,则执行
#如果还有except块,则只能在此处使用此else
最后:
#不管发生什么事都要跑
您试过第二个吗?请浏览下面的链接。解释得很好。
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