mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
Doc: errors tutorial improvements (GH-16269)
Improvements: - Improvements in how try clause works section This suggestion is because the execution continues after *except*, not after *try* but before *except*. I guess this form more clear. - Surrounding some keywords with \*...\* For uniformity the highlighted terms - Adjust the number of chars per line to 80
This commit is contained in:
parent
e603443642
commit
89294e30ff
1 changed files with 21 additions and 21 deletions
|
@ -101,29 +101,29 @@ The :keyword:`try` statement works as follows.
|
||||||
* If no exception occurs, the *except clause* is skipped and execution of the
|
* If no exception occurs, the *except clause* is skipped and execution of the
|
||||||
:keyword:`try` statement is finished.
|
:keyword:`try` statement is finished.
|
||||||
|
|
||||||
* If an exception occurs during execution of the try clause, the rest of the
|
* If an exception occurs during execution of the :keyword:`try` clause, the rest of the
|
||||||
clause is skipped. Then if its type matches the exception named after the
|
clause is skipped. Then, if its type matches the exception named after the
|
||||||
:keyword:`except` keyword, the except clause is executed, and then execution
|
:keyword:`except` keyword, the *except clause* is executed, and then execution
|
||||||
continues after the :keyword:`try` statement.
|
continues after the try/except block.
|
||||||
|
|
||||||
* If an exception occurs which does not match the exception named in the except
|
* If an exception occurs which does not match the exception named in the *except
|
||||||
clause, it is passed on to outer :keyword:`try` statements; if no handler is
|
clause*, it is passed on to outer :keyword:`try` statements; if no handler is
|
||||||
found, it is an *unhandled exception* and execution stops with a message as
|
found, it is an *unhandled exception* and execution stops with a message as
|
||||||
shown above.
|
shown above.
|
||||||
|
|
||||||
A :keyword:`try` statement may have more than one except clause, to specify
|
A :keyword:`try` statement may have more than one *except clause*, to specify
|
||||||
handlers for different exceptions. At most one handler will be executed.
|
handlers for different exceptions. At most one handler will be executed.
|
||||||
Handlers only handle exceptions that occur in the corresponding try clause, not
|
Handlers only handle exceptions that occur in the corresponding *try clause*,
|
||||||
in other handlers of the same :keyword:`!try` statement. An except clause may
|
not in other handlers of the same :keyword:`!try` statement. An *except clause*
|
||||||
name multiple exceptions as a parenthesized tuple, for example::
|
may 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
|
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
|
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
|
*except clause* listing a derived class is not compatible with a base class).
|
||||||
example, the following code will print B, C, D in that order::
|
For example, the following code will print B, C, D in that order::
|
||||||
|
|
||||||
class B(Exception):
|
class B(Exception):
|
||||||
pass
|
pass
|
||||||
|
@ -144,10 +144,10 @@ example, the following code will print B, C, D in that order::
|
||||||
except B:
|
except B:
|
||||||
print("B")
|
print("B")
|
||||||
|
|
||||||
Note that if the except clauses were reversed (with ``except B`` first), it
|
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.
|
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
|
||||||
the exception (allowing a caller to handle the exception as well)::
|
the exception (allowing a caller to handle the exception as well)::
|
||||||
|
@ -167,9 +167,9 @@ the exception (allowing a caller to handle the exception as well)::
|
||||||
raise
|
raise
|
||||||
|
|
||||||
The :keyword:`try` ... :keyword:`except` statement has an optional *else
|
The :keyword:`try` ... :keyword:`except` statement has an optional *else
|
||||||
clause*, which, when present, must follow all except clauses. It is useful for
|
clause*, which, when present, must follow all *except clauses*. It is useful
|
||||||
code that must be executed if the try clause does not raise an exception. For
|
for code that must be executed if the *try clause* does not raise an exception.
|
||||||
example::
|
For example::
|
||||||
|
|
||||||
for arg in sys.argv[1:]:
|
for arg in sys.argv[1:]:
|
||||||
try:
|
try:
|
||||||
|
@ -189,7 +189,7 @@ When an exception occurs, it may have an associated value, also known as the
|
||||||
exception's *argument*. The presence and type of the argument depend on the
|
exception's *argument*. The presence and type of the argument depend on the
|
||||||
exception type.
|
exception type.
|
||||||
|
|
||||||
The except clause may specify a variable after the exception name. The
|
The *except clause* may specify a variable after the exception name. The
|
||||||
variable is bound to an exception instance with the arguments stored in
|
variable is bound to an exception instance with the arguments stored in
|
||||||
``instance.args``. For convenience, the exception instance defines
|
``instance.args``. For convenience, the exception instance defines
|
||||||
:meth:`__str__` so the arguments can be printed directly without having to
|
:meth:`__str__` so the arguments can be printed directly without having to
|
||||||
|
@ -217,8 +217,8 @@ If an exception has arguments, they are printed as the last part ('detail') of
|
||||||
the message for unhandled exceptions.
|
the message for unhandled exceptions.
|
||||||
|
|
||||||
Exception handlers don't just handle exceptions if they occur immediately in the
|
Exception handlers don't just handle exceptions if they occur immediately in the
|
||||||
try clause, but also if they occur inside functions that are called (even
|
*try clause*, but also if they occur inside functions that are called (even
|
||||||
indirectly) in the try clause. For example::
|
indirectly) in the *try clause*. For example::
|
||||||
|
|
||||||
>>> def this_fails():
|
>>> def this_fails():
|
||||||
... x = 1/0
|
... x = 1/0
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue