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

Skip to content

Commit 1aea30a

Browse files
committed
#3113: document exception chaining.
1 parent 0f6de93 commit 1aea30a

3 files changed

Lines changed: 68 additions & 19 deletions

File tree

Doc/library/traceback.rst

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
:mod:`traceback` --- Print or retrieve a stack traceback
32
========================================================
43

@@ -29,29 +28,31 @@ The module defines the following functions:
2928
object to receive the output.
3029

3130

32-
.. function:: print_exception(type, value, traceback[, limit[, file]])
31+
.. function:: print_exception(type, value, traceback[, limit[, file[, chain]]])
3332

3433
Print exception information and up to *limit* stack trace entries from
35-
*traceback* to *file*. This differs from :func:`print_tb` in the following ways:
36-
(1) if *traceback* is not ``None``, it prints a header ``Traceback (most recent
37-
call last):``; (2) it prints the exception *type* and *value* after the stack
38-
trace; (3) if *type* is :exc:`SyntaxError` and *value* has the appropriate
39-
format, it prints the line where the syntax error occurred with a caret
40-
indicating the approximate position of the error.
41-
34+
*traceback* to *file*. This differs from :func:`print_tb` in the following
35+
ways:
4236

43-
.. function:: print_exc([limit[, file]])
37+
* if *traceback* is not ``None``, it prints a header ``Traceback (most recent
38+
call last):``
39+
* it prints the exception *type* and *value* after the stack trace
40+
* if *type* is :exc:`SyntaxError` and *value* has the appropriate format, it
41+
prints the line where the syntax error occurred with a caret indicating the
42+
approximate position of the error.
4443

45-
This is a shorthand for ``print_exception(*sys.exc_info())``.
44+
If *chain* is true (the default), then chained exceptions (the
45+
:attr:`__cause__` or :attr:`__context__` attributes of the exception) will be
46+
printed as well, like the interpreter itself does when printing an unhandled
47+
exception.
4648

4749

48-
.. function:: format_exc([limit])
50+
.. function:: print_exc([limit[, file[, chain]]])
4951

50-
This is like ``print_exc(limit)`` but returns a string instead of printing to a
51-
file.
52+
This is a shorthand for ``print_exception(*sys.exc_info())``.
5253

5354

54-
.. function:: print_last([limit[, file]])
55+
.. function:: print_last([limit[, file[, chain]]])
5556

5657
This is a shorthand for ``print_exception(sys.last_type, sys.last_value,
5758
sys.last_traceback, limit, file)``.
@@ -103,7 +104,7 @@ The module defines the following functions:
103104
occurred is the always last string in the list.
104105

105106

106-
.. function:: format_exception(type, value, tb[, limit])
107+
.. function:: format_exception(type, value, tb[, limit[, chain]])
107108

108109
Format a stack trace and the exception information. The arguments have the
109110
same meaning as the corresponding arguments to :func:`print_exception`. The
@@ -112,6 +113,12 @@ The module defines the following functions:
112113
same text is printed as does :func:`print_exception`.
113114

114115

116+
.. function:: format_exc([limit[, chain]])
117+
118+
This is like ``print_exc(limit)`` but returns a string instead of printing to a
119+
file.
120+
121+
115122
.. function:: format_tb(tb[, limit])
116123

117124
A shorthand for ``format_list(extract_tb(tb, limit))``.

Doc/reference/executionmodel.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ handler and can carry additional information about the exceptional condition.
230230
See also the description of the :keyword:`try` statement in section :ref:`try`
231231
and :keyword:`raise` statement in section :ref:`raise`.
232232

233+
233234
.. rubric:: Footnotes
234235

235236
.. [#] This limitation occurs because the code that is executed by these operations

Doc/reference/simple_stmts.rst

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ The :keyword:`raise` statement
476476
statement: raise
477477
single: exception
478478
pair: raising; exception
479+
single: __traceback__ (exception attribute)
479480

480481
.. productionlist::
481482
raise_stmt: "raise" [`expression` ["from" `expression`]]
@@ -503,9 +504,49 @@ instance, with its traceback set to its argument), like so::
503504

504505
raise RuntimeError("foo occurred").with_traceback(tracebackobj)
505506

506-
.. XXX document exception chaining
507-
508-
The "from" clause is used for exception chaining, which is not documented yet.
507+
.. index:: pair: exception; chaining
508+
__cause__ (exception attribute)
509+
__context__ (exception attribute)
510+
511+
The ``from`` clause is used for exception chaining: if given, the second
512+
*expression* must be another exception class or instance, which will then be
513+
attached to the raised exception as the :attr:`__cause__` attribute (which is
514+
writable). If the raised exception is not handled, both exceptions will be
515+
printed::
516+
517+
>>> try:
518+
... print(1 / 0)
519+
... except Exception as exc:
520+
... raise RuntimeError("Something bad happened") from exc
521+
...
522+
Traceback (most recent call last):
523+
File "<stdin>", line 2, in <module>
524+
ZeroDivisionError: int division or modulo by zero
525+
526+
The above exception was the direct cause of the following exception:
527+
528+
Traceback (most recent call last):
529+
File "<stdin>", line 4, in <module>
530+
RuntimeError: Something bad happened
531+
532+
A similar mechanism works implicitly if an exception is raised inside an
533+
exception handler: the previous exception is then attached as the new
534+
exception's :attr:`__context__` attribute::
535+
536+
>>> try:
537+
... print(1 / 0)
538+
... except:
539+
... raise RuntimeError("Something bad happened")
540+
...
541+
Traceback (most recent call last):
542+
File "<stdin>", line 2, in <module>
543+
ZeroDivisionError: int division or modulo by zero
544+
545+
During handling of the above exception, another exception occurred:
546+
547+
Traceback (most recent call last):
548+
File "<stdin>", line 4, in <module>
549+
RuntimeError: Something bad happened
509550

510551
Additional information on exceptions can be found in section :ref:`exceptions`,
511552
and information about handling exceptions is in section :ref:`try`.

0 commit comments

Comments
 (0)