Skip to content

Commit 0cc2741

Browse files
toonarmycaptainmatrixise
authored andcommitted
Improve clarity of try-return-finally-return (GH-15677)
Clarify execution in try-return-finally-return case.
1 parent 4a12a17 commit 0cc2741

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
@@ -388,15 +388,28 @@ example::
388388
File "<stdin>", line 2, in <module>
389389
KeyboardInterrupt
390390

391-
A *finally clause* is always executed before leaving the :keyword:`try`
392-
statement, whether an exception has occurred or not. When an exception has
393-
occurred in the :keyword:`!try` clause and has not been handled by an
394-
:keyword:`except` clause (or it has occurred in an :keyword:`!except` or
395-
:keyword:`!else` clause), it is re-raised after the :keyword:`finally` clause has
396-
been executed. The :keyword:`!finally` clause is also executed "on the way out"
397-
when any other clause of the :keyword:`!try` statement is left via a
398-
:keyword:`break`, :keyword:`continue` or :keyword:`return` statement. A more
399-
complicated example::
391+
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:
392+
393+
* 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.
394+
395+
* 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.
396+
397+
* 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.
398+
399+
* 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.
400+
401+
For example::
402+
403+
>>> def bool_return(): -> bool:
404+
... try:
405+
... return True
406+
... finally:
407+
... return False
408+
...
409+
>>> bool_return()
410+
False
411+
412+
A more complicated example::
400413

401414
>>> def divide(x, y):
402415
... 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)