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
87170d67
Commit
87170d67
authored
Nov 06, 2016
by
Berker Peksag
Browse files
Options
Browse Files
Download
Plain Diff
Issue #21864: Merge from 3.5
parents
60e49aa7
cea632ec
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
50 deletions
+31
-50
Doc/tutorial/classes.rst
Doc/tutorial/classes.rst
+0
-49
Doc/tutorial/errors.rst
Doc/tutorial/errors.rst
+31
-1
No files found.
Doc/tutorial/classes.rst
View file @
87170d67
...
@@ -744,55 +744,6 @@ object with the method :meth:`m`, and ``m.__func__`` is the function object
...
@@ -744,55 +744,6 @@ object with the method :meth:`m`, and ``m.__func__`` is the function object
corresponding to the method.
corresponding to the method.
.. _tut-exceptionclasses:
Exceptions Are Classes Too
==========================
User-defined exceptions are identified by classes as well. Using this mechanism
it is possible to create extensible hierarchies of exceptions.
There are two new valid (semantic) forms for the :keyword:`raise` statement::
raise Class
raise Instance
In the first form, ``Class`` must be an instance of :class:`type` or of a
class derived from it. The first form is a shorthand for::
raise Class()
A class in an :keyword:`except` clause is compatible with an exception if it is
the same class or a base class thereof (but not the other way around --- an
except clause listing a derived class is not compatible with a base class). For
example, the following code will print B, C, D in that order::
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D:
print("D")
except C:
print("C")
except B:
print("B")
Note that if the except clauses were reversed (with ``except B`` first), it
would have printed B, B, B --- the first matching except clause is triggered.
When an error message is printed for an unhandled exception, the exception's
class name is printed, then a colon and a space, and finally the instance
converted to a string using the built-in function :func:`str`.
.. _tut-iterators:
.. _tut-iterators:
Iterators
Iterators
...
...
Doc/tutorial/errors.rst
View file @
87170d67
...
@@ -120,6 +120,33 @@ name multiple exceptions as a parenthesized tuple, for example::
...
@@ -120,6 +120,33 @@ name multiple exceptions as a parenthesized tuple, for example::
... except (RuntimeError, TypeError, NameError):
... except (RuntimeError, TypeError, NameError):
... pass
... pass
A class in an :keyword:`except` clause is compatible with an exception if it is
the same class or a base class thereof (but not the other way around --- an
except clause listing a derived class is not compatible with a base class). For
example, the following code will print B, C, D in that order::
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D:
print("D")
except C:
print("C")
except B:
print("B")
Note that if the except clauses were reversed (with ``except B`` first), it
would have printed B, B, B --- the first matching except clause is triggered.
The last except clause may omit the exception name(s), to serve as a wildcard.
The last except clause may omit the exception name(s), to serve as a wildcard.
Use this with extreme caution, since it is easy to mask a real programming error
Use this with extreme caution, since it is easy to mask a real programming error
in this way! It can also be used to print an error message and then re-raise
in this way! It can also be used to print an error message and then re-raise
...
@@ -219,7 +246,10 @@ exception to occur. For example::
...
@@ -219,7 +246,10 @@ exception to occur. For example::
The sole argument to :keyword:`raise` indicates the exception to be raised.
The sole argument to :keyword:`raise` indicates the exception to be raised.
This must be either an exception instance or an exception class (a class that
This must be either an exception instance or an exception class (a class that
derives from :class:`Exception`).
derives from :class:`Exception`). If an exception class is passed, it will
be implicitly instantiated by calling its constructor with no arguments::
raise ValueError # shorthand for 'raise ValueError()'
If you need to determine whether an exception was raised but don't intend to
If you need to determine whether an exception was raised but don't intend to
handle it, a simpler form of the :keyword:`raise` statement allows you to
handle it, a simpler form of the :keyword:`raise` statement allows you to
...
...
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