summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZachary Ware <zachary.ware@gmail.com>2014-05-06 14:07:13 (GMT)
committerZachary Ware <zachary.ware@gmail.com>2014-05-06 14:07:13 (GMT)
commit1512143ef044386916dabfbd6f218ca05df2d943 (patch)
tree027f32bee0a464f0cd1f5174d6f18d352572dbea
parent93434890dcff674d94c87598435824909ab18481 (diff)
downloadcpython-1512143ef044386916dabfbd6f218ca05df2d943.zip
cpython-1512143ef044386916dabfbd6f218ca05df2d943.tar.gz
cpython-1512143ef044386916dabfbd6f218ca05df2d943.tar.bz2
Issue #21366: Document the fact that ``return`` in a ``finally`` clause
overrides a ``return`` in the ``try`` suite.
-rw-r--r--Doc/reference/compound_stmts.rst14
1 files changed, 14 insertions, 0 deletions
diff --git a/Doc/reference/compound_stmts.rst b/Doc/reference/compound_stmts.rst
index 56e0769..5371861 100644
--- a/Doc/reference/compound_stmts.rst
+++ b/Doc/reference/compound_stmts.rst
@@ -320,6 +320,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
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`,
and information on using the :keyword:`raise` statement to generate exceptions
may be found in section :ref:`raise`.