Skip to content

Commit 446463f

Browse files
authored
bpo-39534: Doc: Clarify return in finally (GH-18324)
1 parent 227af8e commit 446463f

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

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. If the exception is not handled by an :keyword:`except` clause, 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.
391+
If a :keyword:`finally` clause is present, the :keyword:`!finally`
392+
clause will execute as the last task before the :keyword:`try`
393+
statement completes. The :keyword:`!finally` clause runs whether or
394+
not the :keyword:`!try` statement produces an exception. The following
395+
points discuss more complex cases when an exception occurs:
396+
397+
* If an exception occurs during execution of the :keyword:`!try`
398+
clause, the exception may be handled by an :keyword:`except`
399+
clause. If the exception is not handled by an :keyword:`!except`
400+
clause, the exception is re-raised after the :keyword:`!finally`
401+
clause has been executed.
402+
403+
* An exception could occur during execution of an :keyword:`!except`
404+
or :keyword:`!else` clause. Again, the exception is re-raised after
405+
the :keyword:`!finally` clause has been executed.
406+
407+
* If the :keyword:`!try` statement reaches a :keyword:`break`,
408+
:keyword:`continue` or :keyword:`return` statement, the
409+
:keyword:`!finally` clause will execute just prior to the
410+
:keyword:`!break`, :keyword:`!continue` or :keyword:`!return`
411+
statement's execution.
412+
413+
* If a :keyword:`!finally` clause includes a :keyword:`!return`
414+
statement, the returned value will be the one from the
415+
:keyword:`!finally` clause's :keyword:`!return` statement, not the
416+
value from the :keyword:`!try` clause's :keyword:`!return`
417+
statement.
400418

401419
For example::
402420

0 commit comments

Comments
 (0)