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

Skip to content

Commit f06afe3

Browse files
committed
Issue #14167: Document return statement in finally blocks.
Patch by Yury Selivanov.
2 parents 3165a75 + f158d86 commit f06afe3

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

Doc/reference/compound_stmts.rst

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,12 +307,23 @@ If :keyword:`finally` is present, it specifies a 'cleanup' handler. The
307307
:keyword:`try` clause is executed, including any :keyword:`except` and
308308
:keyword:`else` clauses. If an exception occurs in any of the clauses and is
309309
not handled, the exception is temporarily saved. The :keyword:`finally` clause
310-
is executed. If there is a saved exception, it is re-raised at the end of the
311-
:keyword:`finally` clause. If the :keyword:`finally` clause raises another
312-
exception or executes a :keyword:`return` or :keyword:`break` statement, the
313-
saved exception is set as the context of the new exception. The exception
314-
information is not available to the program during execution of the
315-
:keyword:`finally` clause.
310+
is executed. If there is a saved exception or :keyword:`break` statement,
311+
it is re-raised at the end of the :keyword:`finally` clause. If the
312+
:keyword:`finally` clause raises another exception the saved exception
313+
is set as the context of the new exception; if the :keyword:`finally` clause
314+
executes a :keyword:`return` statement, the saved exception is discarded::
315+
316+
def f():
317+
try:
318+
1/0
319+
finally:
320+
return 42
321+
322+
>>> f()
323+
42
324+
325+
The exception information is not available to the program during execution of
326+
the :keyword:`finally` clause.
316327

317328
.. index::
318329
statement: return

0 commit comments

Comments
 (0)