__cause__和__context__有什么区别? [英] What is the difference between __cause__ and __context__?

查看:69
本文介绍了__cause__和__context__有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些是Python异常的属性,但是我很难理解。 Python的文档对此似乎很安静。我看了文档但是我很困惑。那么,两者之间的区别是什么?如何使用它们?

These are attributes for Python exceptions, but I am having trouble wrapping my head around them. Python's documentation seems rather quiet about this. I took a look at the documentation but am rather confused. So, what is the difference between the two and how are they used?

编辑:在那一点上,它们与 __ traceback__有什么关系,如果有的话?

On that note, how are they related to __traceback__, if at all?

编辑3:我想我只是不理解 __原因__ 。我终于理解了 __ traceback __ __ context __ 。为什么 attribute_error .__ cause __ 不引用 AttributeError()

EDIT 3: I guess I just don't understand __cause__. I finally understand __traceback__ and __context__. Why does attribute_error.__cause__ not refer to AttributeError()?

try:
    raise NameError() from OSError
except NameError as name_error:
    print('name_error.__cause__: %s' % repr(name_error.__cause__))
    print('name_error.__context__: %s' % repr(name_error.__context__))
    print('name_error.__traceback__: %s' % repr(name_error.__traceback__))
    try:
        raise AttributeError()
    except AttributeError as attribute_error:
        print('attribute_error.__cause__: %s' % repr(attribute_error.__cause__))
        print('attribute_error.__context__: %s' % repr(attribute_error.__context__))
        print('attribute_error.__traceback__: %s' % repr(attribute_error.__traceback__))
        raise attribute_error from IndexError

此输出

name_error.__cause__: OSError()
name_error.__context__: None
name_error.__traceback__: <traceback object at 0x000000000346CAC8>
attribute_error.__cause__: None
attribute_error.__context__: NameError()
attribute_error.__traceback__: <traceback object at 0x000000000346CA88>
Traceback (most recent call last):
  File "C:\test\test.py", line 13, in <module>
    raise attribute_error from IndexError
  File "C:\test\test.py", line 8, in <module>
    raise AttributeError()
AttributeError


推荐答案

__ cause __ 是导致异常的原因-由于给定的异常,引发了当前异常。这是直接链接-X抛出了此异常,因此Y必须抛出此异常。

__cause__ is the cause of the exception - due to the given exception, the current exception was raised. This is a direct link - X threw this exception, therefore Y has to throw this exception.

__ context __ 另一方面意味着当前异常是在尝试处理另一个异常时引发的,并且定义了在引发该异常时正在处理的异常。这样一来,您就不会遗漏发生其他异常(因此在此代码中引发异常)的事实。 X抛出了这个异常,在处理它时,Y也被抛出了。

__context__ on the other hand means that the current exception was raised while trying to handle another exception, and defines the exception that was being handled at the time this one was raised. This is so that you don't loose the fact that the other exceptions happend (and hence were at this code to throw the exception) - the context. X threw this exception, while handling it, Y was also thrown.

__ traceback __ 显示了堆栈-各种获得当前代码行所遵循的功能级别。这使您可以查明是什么原因导致了异常。可能会使用它(可能与 __ context __ 一起使用)来查找导致给定错误的原因。

__traceback__ shows you the stack - the various levels of functions that have been followed to get to the current line of code. This allows you to pinpoint what caused the exception. It is likely to be used (potentially in tandem with __context__) to find what caused a given bug.

这篇关于__cause__和__context__有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆