]> granicus.if.org Git - python/commitdiff
Issue #14167: Document return statement in finally blocks.
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 14 Aug 2012 12:44:53 +0000 (15:44 +0300)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Tue, 14 Aug 2012 12:44:53 +0000 (15:44 +0300)
Patch by Yury Selivanov.

Doc/reference/compound_stmts.rst

index c99b65a707197c243ef0c62406d6a1ad52f06c5a..275cfd2a105c9b830ff039ffd81fd40a44376049 100644 (file)
@@ -290,15 +290,28 @@ clause are not handled by the preceding :keyword:`except` clauses.
 
 .. index:: keyword: finally
 
-If :keyword:`finally` is present, it specifies a 'cleanup' handler.  The
-:keyword:`try` clause is executed, including any :keyword:`except` and
-:keyword:`else` clauses.  If an exception occurs in any of the clauses and is
-not handled, the exception is temporarily saved. The :keyword:`finally` clause
-is executed.  If there is a saved exception, it is re-raised at the end of the
-:keyword:`finally` clause. If the :keyword:`finally` clause raises another
-exception or executes a :keyword:`return` or :keyword:`break` statement, the
-saved exception is lost.  The exception information is not available to the
-program during execution of the :keyword:`finally` clause.
+If :keyword:`finally` is present, it specifies a 'cleanup' handler.
+The :keyword:`try` clause is executed, including any :keyword:`except`
+and :keyword:`else` clauses.  If an exception occurs in any of the
+clauses and is not handled, the exception is temporarily saved. The
+:keyword:`finally` clause is executed.  If there is a saved exception
+or :keyword:`break` statement, it is re-raised at the end of the
+:keyword:`finally` clause. If the :keyword:`finally` clause raises
+another exception the saved exception is set as the context of the new
+exception; if the :keyword:`finally` clause executes a
+:keyword:`return` statement, the saved exception is discarded::
+
+    def f():
+        try:
+            1/0
+        finally:
+            return 42
+
+    >>> f()
+    42
+
+The exception information is not available to the program during execution of
+the :keyword:`finally` clause.
 
 .. index::
    statement: return