19

我应该如何“重新抛出”异常,即假设:

  • 我在我的代码中尝试了一些东西,不幸的是它失败了。
  • 我尝试了一些“聪明”的解决方法,但这次也失败了

如果我从(失败的)解决方法中抛出异常,这会让用户感到非常困惑,所以我认为最好重新抛出原始异常(?),并带有它附带的描述性回溯(关于实际问题)...

注意:这方面的激励示例是在调用 时np.log(np.array(['1'], dtype=object)),它会尝试一种机智的解决方法并给出一个AttributeError(它“真的”是 a TypeError)。

我能想到的一种方法就是重新调用有问题的函数,但这似乎是顽固的(一方面,理论上原始函数在第二次调用时可能会表现出一些不同的行为):
好吧,这是一个糟糕的例子,但是这里有...

def f():
    raise Exception("sparrow")

def g():
    raise Exception("coconut")

def a():
    f()

假设我这样做:

try:
    a()
except:
    # attempt witty workaround
    g()
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-4-c76b7509b315> in <module>()
      3 except:
      4     # attempt witty workaround
----> 5     g()
      6

<ipython-input-2-e641f2f9a7dc> in g()
      4
      5 def g():
----> 6     raise Exception("coconut")
      7
      8

Exception: coconut

好吧,问题根本不在于椰子,而在于麻雀:

try:
    a()
except:
    # attempt witty workaround
    try:
        g()
    except:
        # workaround failed, I want to rethrow the exception from calling a()
        a() # ideally don't want to call a() again
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-4-e641f2f9a7dc> in <module>()
     19     except:
     20         # workaround failed, I want to rethrow the exception from calling a()
---> 21         a()  # ideally don't want to call a() again

<ipython-input-3-e641f2f9a7dc> in a()
      8
      9 def a():
---> 10     f()
     11
     12

<ipython-input-1-e641f2f9a7dc> in f()
      1 def f():
----> 2     raise Exception("sparrow")
      3
      4
      5 def g():

Exception: sparrow

有没有标准的方法来处理这个问题,还是我认为它完全错误?

4

7 回答 7

9

如果您想让最终用户看到您从未调用过g(),那么您需要存储第一个错误的回溯,调用第二个函数,然后将原始回溯与原始回溯一起抛出。(否则,在 Python2 中,bare raise 会重新引发第二个异常而不是第一个)。问题是没有 2/3 兼容的方式来引发回溯,因此您必须将 Python 2 版本包装在一个exec语句中(因为它是SyntaxErrorPython 3 中的一个)。

这是一个可以让你做到这一点的函数(我pandas最近将它添加到代码库中):

import sys
if sys.version_info[0] >= 3:
    def raise_with_traceback(exc, traceback=Ellipsis):
        if traceback == Ellipsis:
            _, _, traceback = sys.exc_info()
        raise exc.with_traceback(traceback)
else:
    # this version of raise is a syntax error in Python 3
    exec("""
def raise_with_traceback(exc, traceback=Ellipsis):
    if traceback == Ellipsis:
        _, _, traceback = sys.exc_info()
    raise exc, None, traceback
""")

raise_with_traceback.__doc__ = (
"""Raise exception with existing traceback.
If traceback is not passed, uses sys.exc_info() to get traceback."""
)

然后你可以像这样使用它(为了清楚起见,我还更改了异常类型)。

def f():
    raise TypeError("sparrow")

def g():
    raise ValueError("coconut")

def a():
    f()

try:
    a()
except TypeError as e:
    import sys
    # save the traceback from the original exception
    _, _, tb = sys.exc_info()
    try:
        # attempt witty workaround
        g()
    except:
        raise_with_traceback(e, tb)

而在 Python 2 中,您只能看到a()and f()

Traceback (most recent call last):
  File "test.py", line 40, in <module>
    raise_with_traceback(e, tb)
  File "test.py", line 31, in <module>
    a()
  File "test.py", line 28, in a
    f()
  File "test.py", line 22, in f
    raise TypeError("sparrow")
TypeError: sparrow

但是在 Python 3 中,它仍然注意到还有一个额外的异常,因为您在它的except子句中提出 [这会颠倒错误的顺序并使用户更加困惑]:

Traceback (most recent call last):
  File "test.py", line 38, in <module>
    g()
  File "test.py", line 25, in g
    raise ValueError("coconut")
ValueError: coconut

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 40, in <module>
    raise_with_traceback(e, tb)
  File "test.py", line 6, in raise_with_traceback
    raise exc.with_traceback(traceback)
  File "test.py", line 31, in <module>
    a()
  File "test.py", line 28, in a
    f()
  File "test.py", line 22, in f
    raise TypeError("sparrow")
TypeError: sparrow

如果您绝对希望它看起来像g()Python 2 和 Python 3 中从未发生过异常,您需要首先检查您是否超出了该except子句:

try:
    a()
except TypeError as e:
    import sys
    # save the traceback from the original exception
    _, _, tb = sys.exc_info()
    handled = False
    try:
        # attempt witty workaround
        g()
        handled = True
    except:
        pass
    if not handled:
        raise_with_traceback(e, tb)

这会在 Python 2 中为您提供以下回溯:

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    raise_with_traceback(e, tb)
  File "test.py", line 43, in <module>
    a()
  File "test.py", line 28, in a
    f()
  File "test.py", line 22, in f
    raise TypeError("sparrow")
TypeError: sparrow

而 Python 3 中的这个追溯:

Traceback (most recent call last):
  File "test.py", line 56, in <module>
    raise_with_traceback(e, tb)
  File "test.py", line 6, in raise_with_traceback
    raise exc.with_traceback(traceback)
  File "test.py", line 43, in <module>
    a()
  File "test.py", line 28, in a
    f()
  File "test.py", line 22, in f
    raise TypeError("sparrow")
TypeError: sparrow

它确实添加了一条额外的无用的回溯行,raise exc.with_traceback(traceback)向用户显示 ,但它相对干净。

于 2013-09-05T11:01:42.813 回答
8

这是一些完全疯狂的东西,我不确定它是否会起作用,但它在 python 2 和 3 中都有效。(但是,它确实需要将异常封装到一个函数中......)

def f():
    print ("Fail!")
    raise Exception("sparrow")
def g():
    print ("Workaround fail.")
    raise Exception("coconut")
def a():
    f()

def tryhard():
    ok = False
    try:
        a()
        ok = True
    finally:
        if not ok:
            try:
                g()
                return # "cancels" sparrow Exception by returning from finally
            except:
                pass

>>> tryhard()
Fail!
Workaround fail.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in tryhard
  File "<stdin>", line 2, in a
  File "<stdin>", line 3, in f
Exception: sparrow

这是正确的异常和正确的堆栈跟踪,并且没有黑客行为。

>>> def g(): print "Worked around." # workaround is successful in this case

>>> tryhard()
Fail!
Worked around.

>>> def f(): print "Success!" # normal method works

>>> tryhard()
Success!
于 2013-06-08T17:54:10.797 回答
6

Ian Bicking 有一本很好的关于再加注的入门书。

作为推论,我的规则是只捕获代码知道如何处理的异常。很少有方法真正符合这条规则。例如,如果我正在读取一个文件并抛出一个 IOException,那么该方法几乎不能合理地做。

作为推论,如果您可以返回到良好状态并且您不只是想将用户转储出去,那么在“main”中捕获异常是合理的;这仅在交互式程序中获得。

入门书的相关部分是更新:

try:
    a()
except:
    exc_info = sys.exc_info()
    try:
        g()
    except:
        # If this happens, it clobbers exc_info,
        # which is why we had to save it above
        import traceback
        print >> sys.stderr, "Error in revert_stuff():"
        # py3 print("Error in revert_stuff():", file=sys.stderr)
        traceback.print_exc()
    raise exc_info[0], exc_info[1], exc_info[2]

在 python 3 中,最终的 raise 可以写成

ei = exc_info[1]
ei.filname = exc_info[0]
ei.__traceback__ = exc_info[2]
raise ei
于 2013-06-08T16:27:54.420 回答
4

在 Python 3(特别在 3.3.2 上测试)中,这一切都更好,不需要保存sys.exc_info. 不要在第二个异常处理程序中重新引发原始异常。请注意,第二次尝试失败并在原始处理程序的范围内引发原始,如下所示:

#!python3

try:
    a()
except Exception:
    g_failed = False
    try:
        g()
    except Exception:
        g_failed = True
    raise

Python 3 输出正确地提高了“麻雀”并通过a()和显示回溯f()

Traceback (most recent call last):
  File "x3.py", line 13, in <module>
    a()
  File "x3.py", line 10, in a
    f()
  File "x3.py", line 4, in f
    raise Exception("sparrow")
Exception: sparrow

但是,Python 2 上的相同脚本错误地引发了“椰子”并且只显示g()

Traceback (most recent call last):
  File "x3.py", line 17, in <module>
    g()
  File "x3.py", line 7, in g
    raise Exception("coconut")
Exception: coconut

以下是使 Python 2 正常工作的修改:

#!python2
import sys

try:
    a()
except Exception:
    exc = sys.exc_info()
    try:
        g()
    except Exception:
        raise exc[0], exc[1], exc[2] # Note doesn't care that it is nested.

现在 Python 2 正确地显示了“sparrow”以及两者a()f()回溯:

Traceback (most recent call last):
  File "x2.py", line 14, in <module>
    a()
  File "x2.py", line 11, in a
    f()
  File "x2.py", line 5, in f
    raise Exception("sparrow")
Exception: sparrow
于 2013-06-08T16:56:39.930 回答
3

捕获您的except子句中的错误,然后手动重新提出它。捕获回溯,并通过traceback模块重新打印。

import sys
import traceback

def f():
    raise Exception("sparrow")

def g():
    raise Exception("coconut")

def a():
    f()

try:
    print "trying a"
    a()
except Exception as e:
    print sys.exc_info()
    (_,_,tb) = sys.exc_info()
    print "trying g"
    try:
        g()
    except:
        print "\n".join(traceback.format_tb(tb))
        raise e
于 2013-06-08T16:22:08.613 回答
0

在 Python 3 中,在一个函数中,这可以以一种非常巧妙的方式完成,遵循@Mark Tolonen 的答案,他使用了一个布尔值。您不能在函数外部执行此操作,因为无法跳出外部try语句:该函数是return.

#!python3

def f():
    raise Exception("sparrow")

def g():
    raise Exception("coconut")

def a():
    f()

def h():
    try:
        a()
    except:
        try:
            g()
            return  # Workaround succeeded!
        except:
            pass  # Oh well, that didn't work.
        raise  # Re-raises *first* exception.

h()

这导致:

Traceback (most recent call last):
  File "uc.py", line 23, in <module>
    h()
  File "uc.py", line 14, in h
    a()
  File "uc.py", line 10, in a
    f()
  File "uc.py", line 4, in f
    raise Exception("sparrow")
Exception: sparrow

...如果相反g成功:

def g(): pass

...那么它不会引发异常。

于 2015-08-31T03:44:21.457 回答
-1
try:
    1/0  # will raise ZeroDivisionError
except Exception as first:
    try:
        x/1  # will raise NameError
    except Exception as second:
        raise first  # will re-raise ZeroDivisionError
于 2013-06-08T16:27:46.257 回答