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

Gra*_*row 15 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)
Run Code Online (Sandbox Code Playgroud)

输出是:

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
Run Code Online (Sandbox Code Playgroud)

我希望装饰器能够处理异常但跟踪应该指示x = 1/0行而不是raise行.我怎样才能做到这一点?

小智 18

只需在一个块中使用raise;(即不要提出任何具体的内容raise;)catch来重新引发异常而不"重置"回溯.


Gar*_*erg 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
Run Code Online (Sandbox Code Playgroud)

常见用法如下:

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
Run Code Online (Sandbox Code Playgroud)