Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
dcfe111e
Commit
dcfe111e
authored
Aug 14, 2019
by
Abhilash Raj
Committed by
Miss Islington (bot)
Aug 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37826: Document exception chaining in Python tutorial for errors. (GH-15243)
https://bugs.python.org/issue37826
parent
71662dc2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
0 deletions
+47
-0
Doc/tutorial/errors.rst
Doc/tutorial/errors.rst
+47
-0
No files found.
Doc/tutorial/errors.rst
View file @
dcfe111e
...
...
@@ -267,6 +267,53 @@ re-raise the exception::
NameError: HiThere
.. _tut-exception-chaining:
Exception Chaining
==================
The :keyword:`raise` statement allows an optional :keyword:`from` which enables
chaining exceptions by setting the ``__cause__`` attribute of the raised
exception. For example::
raise RuntimeError from OSError
This can be useful when you are transforming exceptions. For example::
>>> def func():
... raise IOError
...
>>> try:
... func()
... except IOError as exc:
... raise RuntimeError('Failed to open database') from exc
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "<stdin>", line 2, in func
OSError
<BLANKLINE>
The above exception was the direct cause of the following exception:
<BLANKLINE>
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError
The expression following the :keyword:`from` must be either an exception or
``None``. Exception chaining happens automatically when an exception is raised
inside an exception handler or :keyword:`finally` section. Exception chaining
can be disabled by using ``from None`` idiom:
>>> try:
... open('database.sqlite')
... except IOError:
... raise RuntimeError from None
...
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
RuntimeError
.. _tut-userexceptions:
User-defined Exceptions
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment