@@ -420,16 +420,16 @@ is executed. If there is a saved exception it is re-raised at the end of the
420420:keyword: `!finally ` clause. If the :keyword: `!finally ` clause raises another
421421exception, the saved exception is set as the context of the new exception.
422422If the :keyword: `!finally ` clause executes a :keyword: `return `, :keyword: `break `
423- or :keyword: `continue ` statement, the saved exception is discarded::
423+ or :keyword: `continue ` statement, the saved exception is discarded. For example,
424+ this function returns 42.
424425
425- >>> def f():
426- ... try:
427- ... 1/0
428- ... finally:
429- ... return 42
430- ...
431- >>> f()
432- 42
426+ .. code-block ::
427+
428+ def f():
429+ try:
430+ 1/0
431+ finally:
432+ return 42
433433
434434 The exception information is not available to the program during execution of
435435the :keyword: `!finally ` clause.
@@ -446,21 +446,25 @@ statement, the :keyword:`!finally` clause is also executed 'on the way out.'
446446The return value of a function is determined by the last :keyword: `return `
447447statement executed. Since the :keyword: `!finally ` clause always executes, a
448448:keyword: `!return ` statement executed in the :keyword: `!finally ` clause will
449- always be the last one executed::
449+ always be the last one executed. The following function returns 'finally'.
450450
451- >>> def foo():
452- ... try:
453- ... return 'try'
454- ... finally:
455- ... return 'finally'
456- ...
457- >>> foo()
458- 'finally'
451+ .. code-block ::
452+
453+ def foo():
454+ try:
455+ return 'try'
456+ finally:
457+ return 'finally'
459458
460459 .. versionchanged :: 3.8
461460 Prior to Python 3.8, a :keyword: `continue ` statement was illegal in the
462461 :keyword: `!finally ` clause due to a problem with the implementation.
463462
463+ .. versionchanged :: next
464+ The compiler emits a :exc: `SyntaxWarning ` when a :keyword: `return `,
465+ :keyword: `break ` or :keyword: `continue ` appears in a :keyword: `!finally `
466+ block (see :pep: `765 `).
467+
464468
465469.. _with :
466470.. _as :
0 commit comments