Skip to content

Commit d515c61

Browse files
authored
bpo-35790: Correct the description of sys.exc_info() and add a code example (GH-11625)
1 parent 37a6d5f commit d515c61

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Doc/reference/compound_stmts.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,27 @@ Before an except clause's suite is executed, details about the exception are
302302
stored in the :mod:`sys` module and can be accessed via :func:`sys.exc_info`.
303303
:func:`sys.exc_info` returns a 3-tuple consisting of the exception class, the
304304
exception instance and a traceback object (see section :ref:`types`) identifying
305-
the point in the program where the exception occurred. :func:`sys.exc_info`
306-
values are restored to their previous values (before the call) when returning
307-
from a function that handled an exception.
305+
the point in the program where the exception occurred. The details about the
306+
exception accessed via :func:`sys.exc_info` are restored to their previous values
307+
when leaving an exception handler::
308+
309+
>>> print(sys.exc_info())
310+
(None, None, None)
311+
>>> try:
312+
... raise TypeError
313+
... except:
314+
... print(sys.exc_info())
315+
... try:
316+
... raise ValueError
317+
... except:
318+
... print(sys.exc_info())
319+
... print(sys.exc_info())
320+
...
321+
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
322+
(<class 'ValueError'>, ValueError(), <traceback object at 0x10efad040>)
323+
(<class 'TypeError'>, TypeError(), <traceback object at 0x10efad080>)
324+
>>> print(sys.exc_info())
325+
(None, None, None)
308326

309327
.. index::
310328
keyword: else

0 commit comments

Comments
 (0)