2014-10-09 15 views
0

在此鏈接(https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions):終於在Python增加只是下面是說更好的可讀性

一個finally從句離開try語句,是否已經發生異常之前始終執行。

CODE 1:

try: 
    print "Performing an action which may throw an exception." 
except Exception, error: 
    print "An exception was thrown!" 
    print str(error) 
else: 
    print "Everything looks great!" 
finally: 
    print "Finally is called directly after executing the try statement whether an exception is thrown or not." 

OUTPUT 1:

Performing an action which may throw an exception. 
Everything looks great! 
Finally is called directly after executing the try statement whether an exception is thrown or not. 

CODE 2:

try: 
    print "Performing an action which may throw an exception." 
    raise Exception('spam', 'eggs') # This is new 
except Exception, error: 
    print "An exception was thrown!" 
    print str(error) 
else: 
    print "Everything looks great!" 
finally: 
    print "Finally is called directly after executing the try statement whether an exception is thrown or not." 

OUTPUT 2:

Performing an action which may throw an exception. 
An exception was thrown! 
('spam', 'eggs') 
Finally is called directly after executing the try statement whether an exception is thrown or not. 

我從中得到的結果是else只有在沒有異常時纔會執行。

問題:
只是用於更好的可讀性finally
因爲我可以在嘗試後放這個打印語句,就像在這段代碼中一樣。

代碼3:

try: 
    print "Performing an action which may throw an exception." 
    #raise Exception('spam', 'eggs') # with this line or without last print is done 
except Exception, error: 
    print "An exception was thrown!" 
    print str(error) 
else: 
    print "Everything looks great!" 

print "Finally is called directly after executing the try statement whether an exception is thrown or not." 
+1

如果任何代碼*在函數中返回*,或者在循環中使用'continue'或'break',則不會執行print語句。 – 2014-10-09 16:08:13

+0

是不是最後的聲明有清理的目的?此外,[在java中是不是一樣?](http://docs.oracle.com/javase/tutorial/essential/exceptions/finally.html]) – JoErNanO 2014-10-09 16:09:36

回答

5

有些情況下,該finally套房總是執行,但你print聲明不會:

  1. 當在一個未處理例外您try
  2. 當你try是在一個循環(whilefor),您使用的breakcontinue聲明
  3. 當你try在功能和您使用return聲明。

finally套件然後用於保證代碼被執行而不管在try塊會發生什麼情況,即使當到達塊結束之前退出。

比較:

def foo(): 
    try: 
     print "Performing an action which may throw an exception." 
     return 
    except Exception as error: 
     print "An exception occured!", error 
    finally: 
     print "Cleaning up" 

def bar(): 
    try: 
     print "Performing an action which may throw an exception." 
     return 
    except Exception as error: 
     print "An exception occured!", error 
    print "Cleaning up" 

bar()的最後消息是印刷:

>>> foo() 
Performing an action which may throw an exception. 
Cleaning up 
>>> bar() 
Performing an action which may throw an exception. 
+0

'sys.exit'是第四個嗎? (或者是由未處理的異常所覆蓋?) – GP89 2014-10-09 16:13:55

+0

@ GP89:'sys.exit()'拋出一個異常,所以屬於未處理的異常情況。 – 2014-10-09 16:14:29

+0

您能否提供一些代碼示例,其中來自'CODE 3'的打印語句不會被執行,我將會得到它。謝謝 – WebOrCode 2014-10-09 16:15:07

1

的最後代碼塊總是執行,不論是否拋出異常。這在大多數情況下都是如此(我想說的都是,但......)其他語言:C#,Java

其功能是允許執行清理代碼 - 關閉打開的資源,取消分配內存等 - 爲程序繼續/終止其執行以乾淨和安全的方式。

相關問題