@@ -38,36 +38,48 @@ information on defining exceptions is available in the Python Tutorial under
38
38
Exception context
39
39
-----------------
40
40
41
- When raising a new exception while another exception
42
- is already being handled, the new exception's
43
- :attr: `__context__ ` attribute is automatically set to the handled
44
- exception. An exception may be handled when an :keyword: `except ` or
45
- :keyword: `finally ` clause, or a :keyword: `with ` statement, is used.
46
-
47
- This implicit exception context can be
48
- supplemented with an explicit cause by using :keyword: `!from ` with
49
- :keyword: `raise `::
50
-
51
- raise new_exc from original_exc
52
-
53
- The expression following :keyword: `from<raise> ` must be an exception or ``None ``. It
54
- will be set as :attr: `__cause__ ` on the raised exception. Setting
55
- :attr: `__cause__ ` also implicitly sets the :attr: `__suppress_context__ `
56
- attribute to ``True ``, so that using ``raise new_exc from None ``
57
- effectively replaces the old exception with the new one for display
58
- purposes (e.g. converting :exc: `KeyError ` to :exc: `AttributeError `), while
59
- leaving the old exception available in :attr: `__context__ ` for introspection
60
- when debugging.
61
-
62
- The default traceback display code shows these chained exceptions in
63
- addition to the traceback for the exception itself. An explicitly chained
64
- exception in :attr: `__cause__ ` is always shown when present. An implicitly
65
- chained exception in :attr: `__context__ ` is shown only if :attr: `__cause__ `
66
- is :const: `None ` and :attr: `__suppress_context__ ` is false.
67
-
68
- In either case, the exception itself is always shown after any chained
69
- exceptions so that the final line of the traceback always shows the last
70
- exception that was raised.
41
+ .. index :: pair: exception; chaining
42
+ __cause__ (exception attribute)
43
+ __context__ (exception attribute)
44
+ __suppress_context__ (exception attribute)
45
+
46
+ Three attributes on exception objects provide information about the context in
47
+ which an the exception was raised:
48
+
49
+ .. attribute :: BaseException.__context__
50
+ BaseException.__cause__
51
+ BaseException.__suppress_context__
52
+
53
+ When raising a new exception while another exception
54
+ is already being handled, the new exception's
55
+ :attr: `!__context__ ` attribute is automatically set to the handled
56
+ exception. An exception may be handled when an :keyword: `except ` or
57
+ :keyword: `finally ` clause, or a :keyword: `with ` statement, is used.
58
+
59
+ This implicit exception context can be
60
+ supplemented with an explicit cause by using :keyword: `!from ` with
61
+ :keyword: `raise `::
62
+
63
+ raise new_exc from original_exc
64
+
65
+ The expression following :keyword: `from<raise> ` must be an exception or ``None ``. It
66
+ will be set as :attr: `!__cause__ ` on the raised exception. Setting
67
+ :attr: `!__cause__ ` also implicitly sets the :attr: `!__suppress_context__ `
68
+ attribute to ``True ``, so that using ``raise new_exc from None ``
69
+ effectively replaces the old exception with the new one for display
70
+ purposes (e.g. converting :exc: `KeyError ` to :exc: `AttributeError `), while
71
+ leaving the old exception available in :attr: `!__context__ ` for introspection
72
+ when debugging.
73
+
74
+ The default traceback display code shows these chained exceptions in
75
+ addition to the traceback for the exception itself. An explicitly chained
76
+ exception in :attr: `!__cause__ ` is always shown when present. An implicitly
77
+ chained exception in :attr: `!__context__ ` is shown only if :attr: `!__cause__ `
78
+ is :const: `None ` and :attr: `!__suppress_context__ ` is false.
79
+
80
+ In either case, the exception itself is always shown after any chained
81
+ exceptions so that the final line of the traceback always shows the last
82
+ exception that was raised.
71
83
72
84
73
85
Inheriting from built-in exceptions
@@ -126,6 +138,12 @@ The following exceptions are used mostly as base classes for other exceptions.
126
138
tb = sys.exception().__traceback__
127
139
raise OtherException(...).with_traceback(tb)
128
140
141
+ .. attribute :: __traceback__
142
+
143
+ A writable field that holds the
144
+ :ref: `traceback object <traceback-objects >` associated with this
145
+ exception. See also: :ref: `raise `.
146
+
129
147
.. method :: add_note(note)
130
148
131
149
Add the string ``note `` to the exception's notes which appear in the standard
@@ -928,8 +946,10 @@ their subgroups based on the types of the contained exceptions.
928
946
same check that is used in an ``except `` clause.
929
947
930
948
The nesting structure of the current exception is preserved in the result,
931
- as are the values of its :attr: `message `, :attr: `__traceback__ `,
932
- :attr: `__cause__ `, :attr: `__context__ ` and :attr: `__notes__ ` fields.
949
+ as are the values of its :attr: `message `,
950
+ :attr: `~BaseException.__traceback__ `, :attr: `~BaseException.__cause__ `,
951
+ :attr: `~BaseException.__context__ ` and
952
+ :attr: `~BaseException.__notes__ ` fields.
933
953
Empty nested groups are omitted from the result.
934
954
935
955
The condition is checked for all exceptions in the nested exception group,
@@ -952,10 +972,14 @@ their subgroups based on the types of the contained exceptions.
952
972
and :meth: `split ` return instances of the subclass rather
953
973
than :exc: `ExceptionGroup `.
954
974
955
- :meth: `subgroup ` and :meth: `split ` copy the :attr: `__traceback__ `,
956
- :attr: `__cause__ `, :attr: `__context__ ` and :attr: `__notes__ ` fields from
975
+ :meth: `subgroup ` and :meth: `split ` copy the
976
+ :attr: `~BaseException.__traceback__ `,
977
+ :attr: `~BaseException.__cause__ `, :attr: `~BaseException.__context__ ` and
978
+ :attr: `~BaseException.__notes__ ` fields from
957
979
the original exception group to the one returned by :meth: `derive `, so
958
- these fields do not need to be updated by :meth: `derive `. ::
980
+ these fields do not need to be updated by :meth: `derive `.
981
+
982
+ .. doctest ::
959
983
960
984
>>> class MyGroup (ExceptionGroup ):
961
985
... def derive (self , excs ):
0 commit comments