Skip to content

Commit c0acc0e

Browse files
miss-islingtontoonarmycaptain
authored andcommitted
Improve clarity of try-return-finally-return (GH-15677) (GH-15981)
Clarify execution in try-return-finally-return case. (cherry picked from commit 0cc2741) Co-authored-by: toonarmycaptain <toonarmycaptain@hotmail.com>
1 parent 965e53a commit c0acc0e

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Doc/tutorial/errors.rst

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,28 @@ example::
341341
File "<stdin>", line 2, in <module>
342342
KeyboardInterrupt
343343

344-
A *finally clause* is always executed before leaving the :keyword:`try`
345-
statement, whether an exception has occurred or not. When an exception has
346-
occurred in the :keyword:`!try` clause and has not been handled by an
347-
:keyword:`except` clause (or it has occurred in an :keyword:`!except` or
348-
:keyword:`!else` clause), it is re-raised after the :keyword:`finally` clause has
349-
been executed. The :keyword:`!finally` clause is also executed "on the way out"
350-
when any other clause of the :keyword:`!try` statement is left via a
351-
:keyword:`break`, :keyword:`continue` or :keyword:`return` statement. A more
352-
complicated example::
344+
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:
345+
346+
* If an exception occurs during execution of the :keyword:`!try` clause, the exception may be handled by an :keyword:`except` clause. In all cases, the exception is re-raised after the :keyword:`!finally` clause has been executed.
347+
348+
* 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.
349+
350+
* 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.
351+
352+
* 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.
353+
354+
For example::
355+
356+
>>> def bool_return(): -> bool:
357+
... try:
358+
... return True
359+
... finally:
360+
... return False
361+
...
362+
>>> bool_return()
363+
False
364+
365+
A more complicated example::
353366

354367
>>> def divide(x, y):
355368
... try:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Jon Anglin
5858
Michele Angrisano
5959
Ankur Ankan
6060
Heidi Annexstad
61+
David Antonini
6162
Ramchandra Apte
6263
Éric Araujo
6364
Alexandru Ardelean

0 commit comments

Comments
 (0)