Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b4b8f23

Browse files
committed
Merge from 3.3 (issue #15209)
2 parents bb7da03 + 0eee97c commit b4b8f23

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

Doc/library/exceptions.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@ When raising (or re-raising) an exception in an :keyword:`except` clause
3939
new exception is not handled the traceback that is eventually displayed will
4040
include the originating exception(s) and the final exception.
4141

42-
When raising a new exception (rather than using to bare ``raise`` to re-raise
43-
the exception currently being handled), the implicit exception chain can be
44-
made explicit by using :keyword:`from` with :keyword:`raise`. The single
45-
argument to :keyword:`from` must be an exception or ``None``. It will be set
46-
as :attr:`__cause__` on the raised exception. Setting :attr:`__cause__`
47-
also implicitly sets the :attr:`__suppress_context__` attribute to
48-
``True``.
42+
When raising a new exception (rather than using a bare ``raise`` to re-raise
43+
the exception currently being handled), the implicit exception context can be
44+
supplemented with an explicit cause by using :keyword:`from` with
45+
:keyword:`raise`::
46+
47+
raise new_exc from original_exc
48+
49+
The expression following :keyword:`from` must be an exception or ``None``. It
50+
will be set as :attr:`__cause__` on the raised exception. Setting
51+
:attr:`__cause__` also implicitly sets the :attr:`__suppress_context__`
52+
attribute to ``True``, so that using ``raise new_exc from None``
53+
effectively replaces the old exception with the new one for display
54+
purposes (e.g. converting :exc:`KeyError` to :exc:`AttributeError`, while
55+
leaving the old exception available in :attr:`__context__` for introspection
56+
when debugging.
4957

5058
The default traceback display code shows these chained exceptions in
5159
addition to the traceback for the exception itself. An explicitly chained
5260
exception in :attr:`__cause__` is always shown when present. An implicitly
5361
chained exception in :attr:`__context__` is shown only if :attr:`__cause__`
54-
is not set and :attr:`__suppress_context__` is false.
62+
is :const:`None` and :attr:`__suppress_context__` is false.
5563

5664
In either case, the exception itself is always shown after any chained
5765
exceptions so that the final line of the traceback always shows the last

0 commit comments

Comments
 (0)