我知道如何捕捉異常並打印他們回了短信:如何捕捉python中finally語句的消息?
class SelfDefinedException(Exception): pass
try:
message = "Hello World!"
raise SelfDefinedException(message)
except MyDefinedException, e:
print "MyDefinedException", e
這工作好爲止。
但是,如何在'finally'子句中捕獲並打印該消息?
class SelfDefinedException(Exception): pass
try:
message = "Hello World!"
raise SelfDefinedException(message)
except MyDefinedException, e:
print "MyDefinedException", e
finally:
# What goes here? So I can see what went wrong?
從幾個答案我明白,這是不可能的。做這樣的事情可以嗎?
class SelfDefinedException(Exception): pass
try:
message = "Hello World!"
raise SelfDefinedException(message)
except MyDefinedException, e:
print "MyDefinedException", e
except Exception, e:
# Hopefully catches all messages except for the one of MyDefinedException
print "Unexpected Exception raised:", e
你爲什麼想要?不是除外條款不夠好? –
@Winston,我實施了我自己提出的異常處理,但是我想知道是否有其他異常提出,我沒有想到。看我的編輯。 – Aufwind
是的,如果你想捕獲所有其他異常,只要繼續列出除了子句以外的其他異常。 [見文檔](http://docs.python.org/tutorial/errors.html#handling-exceptions) – Dirk