如何在装饰器中捕获异常但允许调用者捕获它?

时间:2010-11-22 20:29:46

标签: python exception decorator

我有一个可能引发异常的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行。我怎么能这样做?

2 个答案:

答案 0 :(得分:17)

只需在raise;块中使用raise;(即不要提出任何特定内容,只是catch)来重新引发异常而不“重置”回溯。

答案 1 :(得分:8)

我刚刚写了一个类似于你正在做的类,但有更多的选择。这是:

class ErrorIgnore(object):
   def __init__(self, errors, errorreturn = None, errorcall = None):
      self.errors = errors
      self.errorreturn = errorreturn
      self.errorcall = errorcall

   def __call__(self, function):
      def returnfunction(*args, **kwargs):
         try:
            return function(*args, **kwargs)
         except Exception as E:
            if type(E) not in self.errors:
               raise E
            if self.errorcall is not None:
               self.errorcall(E, *args, **kwargs)
            return self.errorreturn
      return returnfunction

常用用法如下:

def errorcall(E, *args):
   print 'exception skipped', E

@ErrorIgnore(errors = [ZeroDivisionError, ValueError], errorreturn = None, errorcall = errorcall)
def myfunction(stuff):
   # do stuff
   # return stuff
   # the errors shown are skipped