Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:50:20+00:00 2026-06-06T12:50:20+00:00

These are attributes for Python exceptions, but I am having trouble wrapping my head

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:50:21+00:00Added an answer on June 6, 2026 at 12:50 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1) Isn't it a bad practice to use these attributes on all of my
Castle Validator uses attributes to specify validation rules. How can you hook these up
There seems to be too many attributes/parameters in CSS... I want to know all
I realize that in most cases, it's preferred in Python to just access attributes
I played around with overloading or masking classes in Python. Do the following code
I have a Python list of tuple. Each tuple contains the attributes of a
I have Python classes with object attributes which are only declared as part of
I have searched around for an answer to this but can't find one. When
Why does Python not support a record type natively? It's a matter of having
Playing around with id() . Began with looking at the addresses of identical attributes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.