问题:Python中的“内部异常”(带有追溯)?

我的背景是C#,最近刚开始使用Python编程。当引发异常时,我通常希望将其包装在添加更多信息的另一个异常中,同时仍显示完整的堆栈跟踪。在C#中这很容易,但是如何在Python中做到呢?

例如。在C#中,我将执行以下操作:

try
{
  ProcessFile(filePath);
}
catch (Exception ex)
{
  throw new ApplicationException("Failed to process file " + filePath, ex);
}

在Python中,我可以执行类似的操作:

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file ' + filePath, e)

…但是这丢失了对内部异常的追溯!

编辑:我想同时看到异常消息和堆栈跟踪,并将两者关联起来。也就是说,我想在输出中看到异常X在这里发生,然后异常Y在这里发生-与我在C#中一样。这在Python 2.6中可行吗?到目前为止,看来我能做的最好的(根据Glenn Maynard的回答)是:

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file' + filePath, e), None, sys.exc_info()[2]

这既包括消息,也包括回溯,但是没有显示回溯中哪个异常发生。

My background is in C# and I’ve just recently started programming in Python. When an exception is thrown I typically want to wrap it in another exception that adds more information, while still showing the full stack trace. It’s quite easy in C#, but how do I do it in Python?

Eg. in C# I would do something like this:

try
{
  ProcessFile(filePath);
}
catch (Exception ex)
{
  throw new ApplicationException("Failed to process file " + filePath, ex);
}

In Python I can do something similar:

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file ' + filePath, e)

…but this loses the traceback of the inner exception!

Edit: I’d like to see both exception messages and both stack traces and correlate the two. That is, I want to see in the output that exception X occurred here and then exception Y there – same as I would in C#. Is this possible in Python 2.6? Looks like the best I can do so far (based on Glenn Maynard’s answer) is:

try:
  ProcessFile(filePath)
except Exception as e:
  raise Exception('Failed to process file' + filePath, e), None, sys.exc_info()[2]

This includes both the messages and both the tracebacks, but it doesn’t show which exception occurred where in the traceback.


回答 0

Python 2

这很简单; 将回溯作为第三个引发的参数。

import sys
class MyException(Exception): pass

try:
    raise TypeError("test")
except TypeError, e:
    raise MyException(), None, sys.exc_info()[2]

捕获一个异常并重新引发另一个异常时,请始终执行此操作。

Python 2

It’s simple; pass the traceback as the third argument to raise.

import sys
class MyException(Exception): pass

try:
    raise TypeError("test")
except TypeError, e:
    raise MyException(), None, sys.exc_info()[2]

Always do this when catching one exception and re-raising another.


回答 1

Python 3

在python 3中,您可以执行以下操作:

try:
    raise MyExceptionToBeWrapped("I have twisted my ankle")

except MyExceptionToBeWrapped as e:

    raise MyWrapperException("I'm not in a good shape") from e

这将产生如下内容:

   Traceback (most recent call last):
   ...
   MyExceptionToBeWrapped: ("I have twisted my ankle")

The above exception was the direct cause of the following exception:

   Traceback (most recent call last):
   ...
   MyWrapperException: ("I'm not in a good shape")

Python 3

In python 3 you can do the following:

try:
    raise MyExceptionToBeWrapped("I have twisted my ankle")

except MyExceptionToBeWrapped as e:

    raise MyWrapperException("I'm not in a good shape") from e

This will produce something like this:

   Traceback (most recent call last):
   ...
   MyExceptionToBeWrapped: ("I have twisted my ankle")

The above exception was the direct cause of the following exception:

   Traceback (most recent call last):
   ...
   MyWrapperException: ("I'm not in a good shape")

回答 2

Python 3具有raisefrom子句以链接异常。Glenn的答案对于Python 2.7非常有用,但是它仅使用原始异常的回溯,并丢弃了错误消息和其他详细信息。以下是Python 2.7中的一些示例,这些示例将当前作用域的上下文信息添加到原始异常的错误消息中,而其他细节保持完整。

已知异常类型

try:
    sock_common = xmlrpclib.ServerProxy(rpc_url+'/common')
    self.user_id = sock_common.login(self.dbname, username, self.pwd)
except IOError:
    _, ex, traceback = sys.exc_info()
    message = "Connecting to '%s': %s." % (config['connection'],
                                           ex.strerror)
    raise IOError, (ex.errno, message), traceback

这种raise声明风格将异常类型作为第一个表达式,将元组中的异常类构造函数参数作为第二个表达式,并将回溯作为第三个表达式。如果您运行的版本早于Python 2.2,请参阅中的警告sys.exc_info()

任何异常类型

如果您不知道代码可能必须捕获哪种异常,这是另一个更通用的示例。缺点是它将丢失异常类型,而只会引发RuntimeError。您必须导入traceback模块。

except Exception:
    extype, ex, tb = sys.exc_info()
    formatted = traceback.format_exception_only(extype, ex)[-1]
    message = "Importing row %d, %s" % (rownum, formatted)
    raise RuntimeError, message, tb

修改讯息

如果异常类型允许您向其添加上下文,则这是另一种选择。您可以修改异常的消息,然后重新引发它。

import subprocess

try:
    final_args = ['lsx', '/home']
    s = subprocess.check_output(final_args)
except OSError as ex:
    ex.strerror += ' for command {}'.format(final_args)
    raise

生成以下堆栈跟踪:

Traceback (most recent call last):
  File "/mnt/data/don/workspace/scratch/scratch.py", line 5, in <module>
    s = subprocess.check_output(final_args)
  File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory for command ['lsx', '/home']

您可以看到它显示了check_output()被调用的行,但是异常消息现在包括命令行。

Python 3 has the raisefrom clause to chain exceptions. Glenn’s answer is great for Python 2.7, but it only uses the original exception’s traceback and throws away the error message and other details. Here are some examples in Python 2.7 that add context information from the current scope into the original exception’s error message, but keep other details intact.

Known Exception Type

try:
    sock_common = xmlrpclib.ServerProxy(rpc_url+'/common')
    self.user_id = sock_common.login(self.dbname, username, self.pwd)
except IOError:
    _, ex, traceback = sys.exc_info()
    message = "Connecting to '%s': %s." % (config['connection'],
                                           ex.strerror)
    raise IOError, (ex.errno, message), traceback

That flavour of raise statement takes the exception type as the first expression, the exception class constructor arguments in a tuple as the second expression, and the traceback as the third expression. If you’re running earlier than Python 2.2, see the warnings on sys.exc_info().

Any Exception Type

Here’s another example that’s more general purpose if you don’t know what kind of exceptions your code might have to catch. The downside is that it loses the exception type and just raises a RuntimeError. You have to import the traceback module.

except Exception:
    extype, ex, tb = sys.exc_info()
    formatted = traceback.format_exception_only(extype, ex)[-1]
    message = "Importing row %d, %s" % (rownum, formatted)
    raise RuntimeError, message, tb

Modify the Message

Here’s another option if the exception type will let you add context to it. You can modify the exception’s message and then reraise it.

import subprocess

try:
    final_args = ['lsx', '/home']
    s = subprocess.check_output(final_args)
except OSError as ex:
    ex.strerror += ' for command {}'.format(final_args)
    raise

That generates the following stack trace:

Traceback (most recent call last):
  File "/mnt/data/don/workspace/scratch/scratch.py", line 5, in <module>
    s = subprocess.check_output(final_args)
  File "/usr/lib/python2.7/subprocess.py", line 566, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory for command ['lsx', '/home']

You can see that it shows the line where check_output() was called, but the exception message now includes the command line.


回答 3

Python 3.x中

raise Exception('Failed to process file ' + filePath).with_traceback(e.__traceback__)

简单地

except Exception:
    raise MyException()

它将传播,MyException但如果不处理,则会打印两个异常。

Python 2.x中

raise Exception, 'Failed to process file ' + filePath, e

您可以通过杀死该__context__属性来防止同时打印两个异常。在这里,我编写了一个上下文管理器,使用它来快速捕获和更改您的异常:(有关其工作原理的详细信息,请参见http://docs.python.org/3.1/library/stdtypes.html

try: # Wrap the whole program into the block that will kill __context__.

    class Catcher(Exception):
        '''This context manager reraises an exception under a different name.'''

        def __init__(self, name):
            super().__init__('Failed to process code in {!r}'.format(name))

        def __enter__(self):
            return self

        def __exit__(self, exc_type, exc_val, exc_tb):
            if exc_type is not None:
                self.__traceback__ = exc_tb
                raise self

    ...


    with Catcher('class definition'):
        class a:
            def spam(self):
                # not really pass, but you get the idea
                pass

            lut = [1,
                   3,
                   17,
                   [12,34],
                   5,
                   _spam]


        assert a().lut[-1] == a.spam

    ...


except Catcher as e:
    e.__context__ = None
    raise

In Python 3.x:

raise Exception('Failed to process file ' + filePath).with_traceback(e.__traceback__)

or simply

except Exception:
    raise MyException()

which will propagate MyException but print both exceptions if it will not be handled.

In Python 2.x:

raise Exception, 'Failed to process file ' + filePath, e

You can prevent printing both exceptions by killing the __context__ attribute. Here I write a context manager using that to catch and change your exception on the fly: (see http://docs.python.org/3.1/library/stdtypes.html for expanation of how they work)

try: # Wrap the whole program into the block that will kill __context__.

    class Catcher(Exception):
        '''This context manager reraises an exception under a different name.'''

        def __init__(self, name):
            super().__init__('Failed to process code in {!r}'.format(name))

        def __enter__(self):
            return self

        def __exit__(self, exc_type, exc_val, exc_tb):
            if exc_type is not None:
                self.__traceback__ = exc_tb
                raise self

    ...


    with Catcher('class definition'):
        class a:
            def spam(self):
                # not really pass, but you get the idea
                pass

            lut = [1,
                   3,
                   17,
                   [12,34],
                   5,
                   _spam]


        assert a().lut[-1] == a.spam

    ...


except Catcher as e:
    e.__context__ = None
    raise

回答 4

我认为您无法在Python 2.x中执行此操作,但是与该功能相似的功能是Python 3的一部分。来自PEP 3134

在当今的Python实现中,异常由三部分组成:类型,值和回溯。’sys’模块以三个并行变量exc_type,exc_value和exc_traceback公开当前异常,sys.exc_info()函数返回这三个部分的元组,并且’raise’语句具有接受三个参数的形式这三个部分。处理异常通常需要并行传递这三件事,这可能是乏味且容易出错的。此外,“ except”语句只能提供对值的访问,而不能提供对追溯的访问。将’ traceback ‘属性添加到异常值可以使所有异常信息都可以从一个位置访问。

与C#的比较:

C#中的异常包含一个只读的’InnerException’属性,该属性可能指向另一个异常。它的文档[10]说:“当由于先前的异常Y的直接结果而引发异常X时,X的InnerException属性应包含对Y的引用。” VM不会自动设置此属性。相反,所有异常构造函数都使用可选的“ innerException”参数来对其进行显式设置。在“ 事业 ”属性满足同样的目的的InnerException,但这个PEP提出的“提高”,而不是一直延伸异常的构造函数的新形式。C#还提供了一个GetBaseException方法,该方法直接跳转到InnerException链的末尾。

还要注意,Java,Ruby和Perl 5也不支持这种类型的东西。再次报价:

与其他语言一样,当“ catch” /“ rescue”或“ finally” /“ ensure”子句中发生另一个异常时,Java和Ruby都将丢弃原始异常。Perl 5缺少内置的结构化异常处理。对于Perl 6,RFC 88 [9]提出了一种异常机制,该机制隐式地将链式异常保留在名为@@的数组中。

I don’t think you can do this in Python 2.x, but something similar to this functionality is part of Python 3. From PEP 3134:

In today’s Python implementation, exceptions are composed of three parts: the type, the value, and the traceback. The ‘sys’ module, exposes the current exception in three parallel variables, exc_type, exc_value, and exc_traceback, the sys.exc_info() function returns a tuple of these three parts, and the ‘raise’ statement has a three-argument form accepting these three parts. Manipulating exceptions often requires passing these three things in parallel, which can be tedious and error-prone. Additionally, the ‘except’ statement can only provide access to the value, not the traceback. Adding the ‘traceback‘ attribute to exception values makes all the exception information accessible from a single place.

Comparison to C#:

Exceptions in C# contain a read-only ‘InnerException’ property that may point to another exception. Its documentation [10] says that “When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.” This property is not set by the VM automatically; rather, all exception constructors take an optional ‘innerException’ argument to set it explicitly. The ‘cause‘ attribute fulfills the same purpose as InnerException, but this PEP proposes a new form of ‘raise’ rather than extending the constructors of all exceptions. C# also provides a GetBaseException method that jumps directly to the end of the InnerException chain; this PEP proposes no analog.

Note also that Java, Ruby and Perl 5 don’t support this type of thing either. Quoting again:

As for other languages, Java and Ruby both discard the original exception when another exception occurs in a ‘catch’/’rescue’ or ‘finally’/’ensure’ clause. Perl 5 lacks built-in structured exception handling. For Perl 6, RFC number 88 [9] proposes an exception mechanism that implicitly retains chained exceptions in an array named @@.


回答 5

为了最大程度地兼容Python 2和3,可以raise_from在该six库中使用。 https://six.readthedocs.io/#six.raise_from。这是您的示例(为清晰起见,对其进行了稍微修改):

import six

try:
  ProcessFile(filePath)
except Exception as e:
  six.raise_from(IOError('Failed to process file ' + repr(filePath)), e)

For maximum compatibility between Python 2 and 3, you can use raise_from in the six library. https://six.readthedocs.io/#six.raise_from . Here is your example (slightly modified for clarity):

import six

try:
  ProcessFile(filePath)
except Exception as e:
  six.raise_from(IOError('Failed to process file ' + repr(filePath)), e)

回答 6

您可以使用我的CausedException类在Python 2.x中链接异常(甚至在Python 3中,如果要将多个捕获的异常作为新引发的异常的原因,它也可能很有用)。也许可以帮到您。

You could use my CausedException class to chain exceptions in Python 2.x (and even in Python 3 it can be useful in case you want to give more than one caught exception as cause to a newly raised exception). Maybe it can help you.


回答 7

也许您可以获取相关信息并将其传递出去?我在想类似的东西:

import traceback
import sys
import StringIO

class ApplicationError:
    def __init__(self, value, e):
        s = StringIO.StringIO()
        traceback.print_exc(file=s)
        self.value = (value, s.getvalue())

    def __str__(self):
        return repr(self.value)

try:
    try:
        a = 1/0
    except Exception, e:
        raise ApplicationError("Failed to process file", e)
except Exception, e:
    print e

Maybe you could grab the relevant information and pass it up? I’m thinking something like:

import traceback
import sys
import StringIO

class ApplicationError:
    def __init__(self, value, e):
        s = StringIO.StringIO()
        traceback.print_exc(file=s)
        self.value = (value, s.getvalue())

    def __str__(self):
        return repr(self.value)

try:
    try:
        a = 1/0
    except Exception, e:
        raise ApplicationError("Failed to process file", e)
except Exception, e:
    print e

回答 8

假设:

  • 您需要一个适用于Python 2的解决方案(有关纯Python 3,请参见raise ... from解决方案)
  • 只是想丰富错误消息,例如提供一些其他上下文
  • 需要完整的堆栈跟踪

您可以使用docs https://docs.python.org/3/tutorial/errors.html#raising-exceptions中的简单解决方案:

try:
    raise NameError('HiThere')
except NameError:
    print 'An exception flew by!' # print or log, provide details about context
    raise # reraise the original exception, keeping full stack trace

输出:

An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
NameError: HiThere

看起来关键是简化的“ raise”关键字,它独立存在。这将重新引发except块中的Exception。

Assuming:

  • you need a solution, which works for Python 2 (for pure Python 3 see raise ... from solution)
  • just want to enrich the error message, e.g. providing some additional context
  • need the full stack trace

you can use a simple solution from the docs https://docs.python.org/3/tutorial/errors.html#raising-exceptions:

try:
    raise NameError('HiThere')
except NameError:
    print 'An exception flew by!' # print or log, provide details about context
    raise # reraise the original exception, keeping full stack trace

The output:

An exception flew by!
Traceback (most recent call last):
  File "<stdin>", line 2, in ?
NameError: HiThere

It looks like the key piece is the simplified ‘raise’ keyword that stands alone. That will re-raise the Exception in the except block.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。