@@ -39,19 +39,27 @@ When raising (or re-raising) an exception in an :keyword:`except` clause
3939new exception is not handled the traceback that is eventually displayed will
4040include 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
5058The default traceback display code shows these chained exceptions in
5159addition to the traceback for the exception itself. An explicitly chained
5260exception in :attr: `__cause__ ` is always shown when present. An implicitly
5361chained 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
5664In either case, the exception itself is always shown after any chained
5765exceptions so that the final line of the traceback always shows the last
0 commit comments