@@ -420,16 +420,16 @@ is executed. If there is a saved exception it is re-raised at the end of the
420
420
:keyword: `!finally ` clause. If the :keyword: `!finally ` clause raises another
421
421
exception, the saved exception is set as the context of the new exception.
422
422
If 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.
424
425
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
433
433
434
434
The exception information is not available to the program during execution of
435
435
the :keyword: `!finally ` clause.
@@ -446,21 +446,25 @@ statement, the :keyword:`!finally` clause is also executed 'on the way out.'
446
446
The return value of a function is determined by the last :keyword: `return `
447
447
statement executed. Since the :keyword: `!finally ` clause always executes, a
448
448
: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'.
450
450
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'
459
458
460
459
.. versionchanged :: 3.8
461
460
Prior to Python 3.8, a :keyword: `continue ` statement was illegal in the
462
461
:keyword: `!finally ` clause due to a problem with the implementation.
463
462
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
+
464
468
465
469
.. _with :
466
470
.. _as :
0 commit comments