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

Skip to content

Commit e2d933b

Browse files
committed
Issue python#21864: Merge from 3.6
2 parents 85bcf37 + 87170d6 commit e2d933b

2 files changed

Lines changed: 31 additions & 50 deletions

File tree

Doc/tutorial/classes.rst

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -744,55 +744,6 @@ object with the method :meth:`m`, and ``m.__func__`` is the function object
744744
corresponding to the method.
745745

746746

747-
.. _tut-exceptionclasses:
748-
749-
Exceptions Are Classes Too
750-
==========================
751-
752-
User-defined exceptions are identified by classes as well. Using this mechanism
753-
it is possible to create extensible hierarchies of exceptions.
754-
755-
There are two new valid (semantic) forms for the :keyword:`raise` statement::
756-
757-
raise Class
758-
759-
raise Instance
760-
761-
In the first form, ``Class`` must be an instance of :class:`type` or of a
762-
class derived from it. The first form is a shorthand for::
763-
764-
raise Class()
765-
766-
A class in an :keyword:`except` clause is compatible with an exception if it is
767-
the same class or a base class thereof (but not the other way around --- an
768-
except clause listing a derived class is not compatible with a base class). For
769-
example, the following code will print B, C, D in that order::
770-
771-
class B(Exception):
772-
pass
773-
class C(B):
774-
pass
775-
class D(C):
776-
pass
777-
778-
for cls in [B, C, D]:
779-
try:
780-
raise cls()
781-
except D:
782-
print("D")
783-
except C:
784-
print("C")
785-
except B:
786-
print("B")
787-
788-
Note that if the except clauses were reversed (with ``except B`` first), it
789-
would have printed B, B, B --- the first matching except clause is triggered.
790-
791-
When an error message is printed for an unhandled exception, the exception's
792-
class name is printed, then a colon and a space, and finally the instance
793-
converted to a string using the built-in function :func:`str`.
794-
795-
796747
.. _tut-iterators:
797748

798749
Iterators

Doc/tutorial/errors.rst

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,33 @@ name multiple exceptions as a parenthesized tuple, for example::
120120
... except (RuntimeError, TypeError, NameError):
121121
... pass
122122

123+
A class in an :keyword:`except` clause is compatible with an exception if it is
124+
the same class or a base class thereof (but not the other way around --- an
125+
except clause listing a derived class is not compatible with a base class). For
126+
example, the following code will print B, C, D in that order::
127+
128+
class B(Exception):
129+
pass
130+
131+
class C(B):
132+
pass
133+
134+
class D(C):
135+
pass
136+
137+
for cls in [B, C, D]:
138+
try:
139+
raise cls()
140+
except D:
141+
print("D")
142+
except C:
143+
print("C")
144+
except B:
145+
print("B")
146+
147+
Note that if the except clauses were reversed (with ``except B`` first), it
148+
would have printed B, B, B --- the first matching except clause is triggered.
149+
123150
The last except clause may omit the exception name(s), to serve as a wildcard.
124151
Use this with extreme caution, since it is easy to mask a real programming error
125152
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::
219246

220247
The sole argument to :keyword:`raise` indicates the exception to be raised.
221248
This must be either an exception instance or an exception class (a class that
222-
derives from :class:`Exception`).
249+
derives from :class:`Exception`). If an exception class is passed, it will
250+
be implicitly instantiated by calling its constructor with no arguments::
251+
252+
raise ValueError # shorthand for 'raise ValueError()'
223253

224254
If you need to determine whether an exception was raised but don't intend to
225255
handle it, a simpler form of the :keyword:`raise` statement allows you to

0 commit comments

Comments
 (0)