diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2020-02-06 22:23:04 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-06 22:23:04 (GMT) |
commit | 97e00b3c52796cb54dd0a50548760579b9cb7b3a (patch) | |
tree | 4952806ab3181e1aa4724f05ecc2b867c29922f1 /Doc/tutorial/errors.rst | |
parent | 30096c9365fcb7cc3c38c4d161eaf8f61ae5cfea (diff) | |
download | cpython-97e00b3c52796cb54dd0a50548760579b9cb7b3a.zip cpython-97e00b3c52796cb54dd0a50548760579b9cb7b3a.tar.gz cpython-97e00b3c52796cb54dd0a50548760579b9cb7b3a.tar.bz2 |
bpo-39534: Doc: Clarify return in finally (GH-18324)
(cherry picked from commit 446463f8dbce0556be8020914f37089b63bb8ab6)
Co-authored-by: Julien Palard <julien@palard.fr>
Diffstat (limited to 'Doc/tutorial/errors.rst')
-rw-r--r-- | Doc/tutorial/errors.rst | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst index 4bc7184..27c67c6 100644 --- a/Doc/tutorial/errors.rst +++ b/Doc/tutorial/errors.rst @@ -341,15 +341,33 @@ example:: File "<stdin>", line 2, in <module> KeyboardInterrupt -If a :keyword:`finally` clause is present, the :keyword:`finally` clause will execute as the last task before the :keyword:`try` statement completes. The :keyword:`finally` clause runs whether or not the :keyword:`try` statement produces an exception. The following points discuss more complex cases when an exception occurs: - -* If an exception occurs during execution of the :keyword:`!try` clause, the exception may be handled by an :keyword:`except` clause. If the exception is not handled by an :keyword:`except` clause, the exception is re-raised after the :keyword:`!finally` clause has been executed. - -* An exception could occur during execution of an :keyword:`!except` or :keyword:`!else` clause. Again, the exception is re-raised after the :keyword:`!finally` clause has been executed. - -* If the :keyword:`!try` statement reaches a :keyword:`break`, :keyword:`continue` or :keyword:`return` statement, the :keyword:`finally` clause will execute just prior to the :keyword:`break`, :keyword:`continue` or :keyword:`return` statement's execution. - -* If a :keyword:`finally` clause includes a :keyword:`return` statement, the :keyword:`finally` clause's :keyword:`return` statement will execute before, and instead of, the :keyword:`return` statement in a :keyword:`try` clause. +If a :keyword:`finally` clause is present, the :keyword:`!finally` +clause will execute as the last task before the :keyword:`try` +statement completes. The :keyword:`!finally` clause runs whether or +not the :keyword:`!try` statement produces an exception. The following +points discuss more complex cases when an exception occurs: + +* If an exception occurs during execution of the :keyword:`!try` + clause, the exception may be handled by an :keyword:`except` + clause. If the exception is not handled by an :keyword:`!except` + clause, the exception is re-raised after the :keyword:`!finally` + clause has been executed. + +* An exception could occur during execution of an :keyword:`!except` + or :keyword:`!else` clause. Again, the exception is re-raised after + the :keyword:`!finally` clause has been executed. + +* If the :keyword:`!try` statement reaches a :keyword:`break`, + :keyword:`continue` or :keyword:`return` statement, the + :keyword:`!finally` clause will execute just prior to the + :keyword:`!break`, :keyword:`!continue` or :keyword:`!return` + statement's execution. + +* If a :keyword:`!finally` clause includes a :keyword:`!return` + statement, the returned value will be the one from the + :keyword:`!finally` clause's :keyword:`!return` statement, not the + value from the :keyword:`!try` clause's :keyword:`!return` + statement. For example:: |