summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2022-10-05 21:51:10 (GMT)
committerGitHub <noreply@github.com>2022-10-05 21:51:10 (GMT)
commit9f1c9b1a7a04513fcc8a679fb1075701f37ab621 (patch)
tree7a4640bbf480f23915d4508d3104c20a673cdfa3 /Doc
parent3b0f2a7ff068f24198b848f25662078c4223b10a (diff)
downloadcpython-9f1c9b1a7a04513fcc8a679fb1075701f37ab621.zip
cpython-9f1c9b1a7a04513fcc8a679fb1075701f37ab621.tar.gz
cpython-9f1c9b1a7a04513fcc8a679fb1075701f37ab621.tar.bz2
[3.10] gh-97654: Add auto exception chaining example to tutorial (GH-97703) (#97884)
Add auto exception chaining example to tutorial (cherry picked from commit 395b66a0ae5237eec195ca97daaaf8563706ed34) Co-authored-by: Shahriar Heidrich <smheidrich@weltenfunktion.de>
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tutorial/errors.rst28
1 files changed, 23 insertions, 5 deletions
diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst
index 3f09db2..ecfb612 100644
--- a/Doc/tutorial/errors.rst
+++ b/Doc/tutorial/errors.rst
@@ -275,8 +275,27 @@ re-raise the exception::
Exception Chaining
==================
-The :keyword:`raise` statement allows an optional :keyword:`from<raise>` which enables
-chaining exceptions. For example::
+If an unhandled exception occurs inside an :keyword:`except` section, it will
+have the exception being handled attached to it and included in the error
+message::
+
+ >>> try:
+ ... open("database.sqlite")
+ ... except OSError:
+ ... raise RuntimeError("unable to handle error")
+ ...
+ Traceback (most recent call last):
+ File "<stdin>", line 2, in <module>
+ FileNotFoundError: [Errno 2] No such file or directory: 'database.sqlite'
+ <BLANKLINE>
+ During handling of the above exception, another exception occurred:
+ <BLANKLINE>
+ Traceback (most recent call last):
+ File "<stdin>", line 4, in <module>
+ RuntimeError: unable to handle error
+
+To indicate that an exception is a direct consequence of another, the
+:keyword:`raise` statement allows an optional :keyword:`from<raise>` clause::
# exc must be exception instance or None.
raise RuntimeError from exc
@@ -302,9 +321,8 @@ This can be useful when you are transforming exceptions. For example::
File "<stdin>", line 4, in <module>
RuntimeError: Failed to open database
-Exception chaining happens automatically when an exception is raised inside an
-:keyword:`except` or :keyword:`finally` section. This can be
-disabled by using ``from None`` idiom:
+It also allows disabling automatic exception chaining using the ``from None``
+idiom::
>>> try:
... open('database.sqlite')