2017-02-16 30 views
-2

在Python的try,except塊,爲什麼else需要存在,如果我可以只使用沒有說明符的except:期望沒有說明符vs其他之間的區別?

+1

您是否嘗試過的第二個? – khelwood

+0

瀏覽下面的鏈接。恰好解釋道。 http://stackoverflow.com/questions/16138232/is-it-a-good-practice-to-use-try-except-else-in-python –

回答

2

好像你瞭解try,except,elsefinally是如何關閉的。

下面是它們是如何協同工作的總結,從看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 
相關問題