Skip to content

Commit 83efed9

Browse files
bpo-39534: Doc: Clarify return in finally (GH-18324)
(cherry picked from commit 446463f) Co-authored-by: Julien Palard <[email protected]>
1 parent 705d271 commit 83efed9

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

Doc/tutorial/errors.rst

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

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. If the exception is not handled by an :keyword:`except` clause, 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.
344+
If a :keyword:`finally` clause is present, the :keyword:`!finally`
345+
clause will execute as the last task before the :keyword:`try`
346+
statement completes. The :keyword:`!finally` clause runs whether or
347+
not the :keyword:`!try` statement produces an exception. The following
348+
points discuss more complex cases when an exception occurs:
349+
350+
* If an exception occurs during execution of the :keyword:`!try`
351+
clause, the exception may be handled by an :keyword:`except`
352+
clause. If the exception is not handled by an :keyword:`!except`
353+
clause, the exception is re-raised after the :keyword:`!finally`
354+
clause has been executed.
355+
356+
* An exception could occur during execution of an :keyword:`!except`
357+
or :keyword:`!else` clause. Again, the exception is re-raised after
358+
the :keyword:`!finally` clause has been executed.
359+
360+
* If the :keyword:`!try` statement reaches a :keyword:`break`,
361+
:keyword:`continue` or :keyword:`return` statement, the
362+
:keyword:`!finally` clause will execute just prior to the
363+
:keyword:`!break`, :keyword:`!continue` or :keyword:`!return`
364+
statement's execution.
365+
366+
* If a :keyword:`!finally` clause includes a :keyword:`!return`
367+
statement, the returned value will be the one from the
368+
:keyword:`!finally` clause's :keyword:`!return` statement, not the
369+
value from the :keyword:`!try` clause's :keyword:`!return`
370+
statement.
353371

354372
For example::
355373

0 commit comments

Comments
 (0)