mirror of
https://github.com/python/cpython.git
synced 2025-11-02 11:08:57 +00:00
Issue #21366: Document the fact that `return in a finally` clause
overrides a ``return`` in the ``try`` suite.
This commit is contained in:
commit
4e6688d2b2
1 changed files with 14 additions and 0 deletions
|
|
@ -337,6 +337,20 @@ statement, the :keyword:`finally` clause is also executed 'on the way out.' A
|
||||||
reason is a problem with the current implementation --- this restriction may be
|
reason is a problem with the current implementation --- this restriction may be
|
||||||
lifted in the future).
|
lifted in the future).
|
||||||
|
|
||||||
|
The return value of a function is determined by the last :keyword:`return`
|
||||||
|
statement executed. Since the :keyword:`finally` clause always executes, a
|
||||||
|
:keyword:`return` statement executed in the :keyword:`finally` clause will
|
||||||
|
always be the last one executed::
|
||||||
|
|
||||||
|
>>> def foo():
|
||||||
|
... try:
|
||||||
|
... return 'try'
|
||||||
|
... finally:
|
||||||
|
... return 'finally'
|
||||||
|
...
|
||||||
|
>>> foo()
|
||||||
|
'finally'
|
||||||
|
|
||||||
Additional information on exceptions can be found in section :ref:`exceptions`,
|
Additional information on exceptions can be found in section :ref:`exceptions`,
|
||||||
and information on using the :keyword:`raise` statement to generate exceptions
|
and information on using the :keyword:`raise` statement to generate exceptions
|
||||||
may be found in section :ref:`raise`.
|
may be found in section :ref:`raise`.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue