-2
在Python的try
,except
塊,爲什麼else
需要存在,如果我可以只使用沒有說明符的except:
?期望沒有說明符vs其他之間的區別?
在Python的try
,except
塊,爲什麼else
需要存在,如果我可以只使用沒有說明符的except:
?期望沒有說明符vs其他之間的區別?
好像你瞭解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
您是否嘗試過的第二個? – khelwood
瀏覽下面的鏈接。恰好解釋道。 http://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python –