27

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?

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

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

This outputs

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
1
  • 2
    The doc is thin because these are more or less internal variables that most Pythononeers do not have to even know about. The reason to mention them at all is a) they are visible and b) people who catch one error and raise another may need to know. Commented Jul 4, 2012 at 1:19

1 Answer 1

33

__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__ 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 lose the fact that the other exceptions happened (and hence were at this code to throw the exception) - the context. X threw this exception, while handling it, Y was also thrown.

__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.

3
  • I think I'm starting to get it. Do you mind providing a brief code snippet demonstrating what each of these are at each location in the code? Commented Jun 27, 2012 at 23:05
  • 1
    Okay, so I "moved the goalposts back". Now, I'm just asking for a bit of clarification. Check out my latest edit. Commented Jun 27, 2012 at 23:38
  • 1
    Because from is what gives __cause__ it's value. You are printing __cause__ before anything is there, and your raise statement with the from part is the wrong way round. Commented Jun 28, 2012 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.