从方法中重新引发异常作为参数

re-raise exception from method as argument

我有一个方法需要一些包装,称为焦耳,所以我将那个 joule 方法包装在一个名为 respond 的包装器中(您很快就会看到):

someclass.respond(somemodule.joule(someArgument, someStrategy), 202)

我有一个名为 respond 的包装器:

@classmethod
def respond(cls, method, successStatus):
    try:
        method, successStatus

    except Exception as e:
        return {
            'status': 500,
            'message': str(e)
        }

调用并引发 Exception 的实际方法:

def joule(params, strategy):
    try:
        return strategy(params)

    finally:
        session.rollback()
        conn.execute('UNLOCK TABLES')

出于某种原因,重新引发的异常似乎没有被响应包装器捕获!你们能帮我理解我在这里做错了什么吗?

如果这有帮助,sqlalchemy 抛出的异常是(请注意,这是为正确处理异常而强制创建的场景):

ProgrammingError: (ProgrammingError) (1146, u"Table 'matrix.vmop_queue' doesn't exist") 'LOCK TABLES vmop_queue WRITE' ()

您误解了异常处理的工作原理。异常处理在被调用函数的堆栈帧上运行。

在您给出的示例中,someclass.respond 实际上并没有调用 somemodule.joule,而是您在示例中所写的那一行,这是一些外部上下文,是接收未捕获的地方例外。因此 someclass.respond 不可能处理 somemodule.joule.

抛出的异常

还有其他方法可以实现您想要完成的目标,但我需要更多背景信息才能为您提供更好的建议。

为了更具体一点,假设 foo 包含您提供的示例行:

def foo():
    someclass.respond(somemodule.joule(someArgument, someStrategy), 202)

您可以将 try 块添加到 foo 以处理 somemodule.joule 抛出的异常。这看起来像这样:

def foo():
    try:
        someclass.respond(somemodule.joule(someArgument, someStrategy), 202)
    except Exception as e:
        pass # do something with the exception here

或者,如果 someclass.respond 的全部目的是处理这个异常,那么你应该将 somemodule.joule 的调用移到 里面 of someclass.respond.您甚至可以通过不止一种方式来做到这一点。您通常可以采用一个函数及其参数,并将该函数应用于 someclass.respond 内部的参数,或者您可以直接在 someclass.respond.

内部进行调用

让我们采用第一种方法,因为您已经说过不想重复异常处理。我将这个新方法称为 exception_catcher:

def exception_catcher(func, *args):
    try:
        return func(*args)
    except Exception as e:
        pass # do whatever you want to the exception

现在 foo 上下文将如下所示:

def foo():
    exception_catcher(somemodule.joule, someArgument, someStrategy)

注意 exception_catchersomemodule.joule 作为参数,其余参数将从 exception_catcher.

传递给 somemodule.joule