我有一個可能會引發異常的python函數。調用者捕獲異常並處理它。現在我想添加一個裝飾器到那個函數,也捕獲異常,做一些處理,但然後重新引發異常,以允許原始調用者來處理它。這是有效的,除了當原始調用者從異常中顯示調用堆棧時,它顯示了修飾器中重新生成的行,而不是它最初發生的位置。示例代碼:如何在裝飾器中捕獲異常但允許調用者抓住它?
import sys,traceback
def mydec(func):
def dec():
try:
func()
except Exception,e:
print 'Decorator handled exception %s' % e
raise e
return dec
@mydec
def myfunc():
x = 1/0
try:
myfunc()
except Exception,e:
print 'Exception: %s' % e
type,value,tb = sys.exc_info()
traceback.print_tb(tb)
輸出是:
Decorator handled exception integer division or modulo by zero
Exception: integer division or modulo by zero
File "allbug.py", line 20, in <module>
myfunc()
File "allbug.py", line 9, in dec
raise e
我想裝飾到能夠處理的異常,但回溯應註明x = 1/0
線,而不是raise
線。我怎樣才能做到這一點?
只要是明確的,只要用一個赤裸裸的「提高」的語句。不要指定要提出的內容。 – drxzcl 2010-11-22 20:34:10