From 47e9cfe36a042cde8675d4102b569cd1ebcd6df1 Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Mon, 3 Feb 2020 09:16:06 +0100 Subject: [PATCH 1/3] Doc: Rewording return statements in finally clauses. --- Doc/tutorial/errors.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst index 8f86eca02483a2..fd7ccfc335f07e 100644 --- a/Doc/tutorial/errors.rst +++ b/Doc/tutorial/errors.rst @@ -396,7 +396,7 @@ If a :keyword:`finally` clause is present, the :keyword:`finally` clause will ex * If the :keyword:`!try` statement reaches a :keyword:`break`, :keyword:`continue` or :keyword:`return` statement, the :keyword:`finally` clause will execute just prior to the :keyword:`break`, :keyword:`continue` or :keyword:`return` statement's execution. -* If a :keyword:`finally` clause includes a :keyword:`return` statement, the :keyword:`finally` clause's :keyword:`return` statement will execute before, and instead of, the :keyword:`return` statement in a :keyword:`try` clause. +* If a :keyword:`finally` clause includes a :keyword:`return` statement, the returned value will be the one from the :keyword:`finally` clause's :keyword:`return` statement, not the value from the :keyword:`try` clause's :keyword:`return` statement. For example:: From 8d28654eb1890ca02d38edf9abfbe95b2e2ad38e Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Mon, 3 Feb 2020 09:16:28 +0100 Subject: [PATCH 2/3] Rewrap this paragraph. --- Doc/tutorial/errors.rst | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst index fd7ccfc335f07e..0cce924af2f970 100644 --- a/Doc/tutorial/errors.rst +++ b/Doc/tutorial/errors.rst @@ -388,15 +388,32 @@ example:: File "", line 2, in KeyboardInterrupt -If a :keyword:`finally` clause is present, the :keyword:`finally` clause will execute as the last task before the :keyword:`try` statement completes. The :keyword:`finally` clause runs whether or not the :keyword:`try` statement produces an exception. The following points discuss more complex cases when an exception occurs: - -* If an exception occurs during execution of the :keyword:`!try` clause, the exception may be handled by an :keyword:`except` clause. If the exception is not handled by an :keyword:`except` clause, the exception is re-raised after the :keyword:`!finally` clause has been executed. - -* An exception could occur during execution of an :keyword:`!except` or :keyword:`!else` clause. Again, the exception is re-raised after the :keyword:`!finally` clause has been executed. - -* If the :keyword:`!try` statement reaches a :keyword:`break`, :keyword:`continue` or :keyword:`return` statement, the :keyword:`finally` clause will execute just prior to the :keyword:`break`, :keyword:`continue` or :keyword:`return` statement's execution. - -* If a :keyword:`finally` clause includes a :keyword:`return` statement, the returned value will be the one from the :keyword:`finally` clause's :keyword:`return` statement, not the value from the :keyword:`try` clause's :keyword:`return` statement. +If a :keyword:`finally` clause is present, the :keyword:`finally` +clause will execute as the last task before the :keyword:`try` +statement completes. The :keyword:`finally` clause runs whether or not +the :keyword:`try` statement produces an exception. The following +points discuss more complex cases when an exception occurs: + +* If an exception occurs during execution of the :keyword:`!try` + clause, the exception may be handled by an :keyword:`except` + clause. If the exception is not handled by an :keyword:`except` + clause, the exception is re-raised after the :keyword:`!finally` + clause has been executed. + +* An exception could occur during execution of an :keyword:`!except` + or :keyword:`!else` clause. Again, the exception is re-raised after + the :keyword:`!finally` clause has been executed. + +* If the :keyword:`!try` statement reaches a :keyword:`break`, + :keyword:`continue` or :keyword:`return` statement, the + :keyword:`finally` clause will execute just prior to the + :keyword:`break`, :keyword:`continue` or :keyword:`return` + statement's execution. + +* If a :keyword:`finally` clause includes a :keyword:`return` + statement, the returned value will be the one from the + :keyword:`finally` clause's :keyword:`return` statement, not the + value from the :keyword:`try` clause's :keyword:`return` statement. For example:: From 33a0fd39ac39205c3c7d673c06622214b9c01f6d Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Mon, 3 Feb 2020 17:39:58 +0100 Subject: [PATCH 3/3] Remove a few links, thanks to an idea from Cheryl. --- Doc/tutorial/errors.rst | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Doc/tutorial/errors.rst b/Doc/tutorial/errors.rst index 0cce924af2f970..ab23df9f3ff9aa 100644 --- a/Doc/tutorial/errors.rst +++ b/Doc/tutorial/errors.rst @@ -388,15 +388,15 @@ example:: File "", line 2, in KeyboardInterrupt -If a :keyword:`finally` clause is present, the :keyword:`finally` +If a :keyword:`finally` clause is present, the :keyword:`!finally` clause will execute as the last task before the :keyword:`try` -statement completes. The :keyword:`finally` clause runs whether or not -the :keyword:`try` statement produces an exception. The following +statement completes. The :keyword:`!finally` clause runs whether or +not the :keyword:`!try` statement produces an exception. The following points discuss more complex cases when an exception occurs: * If an exception occurs during execution of the :keyword:`!try` clause, the exception may be handled by an :keyword:`except` - clause. If the exception is not handled by an :keyword:`except` + clause. If the exception is not handled by an :keyword:`!except` clause, the exception is re-raised after the :keyword:`!finally` clause has been executed. @@ -406,14 +406,15 @@ points discuss more complex cases when an exception occurs: * If the :keyword:`!try` statement reaches a :keyword:`break`, :keyword:`continue` or :keyword:`return` statement, the - :keyword:`finally` clause will execute just prior to the - :keyword:`break`, :keyword:`continue` or :keyword:`return` + :keyword:`!finally` clause will execute just prior to the + :keyword:`!break`, :keyword:`!continue` or :keyword:`!return` statement's execution. -* If a :keyword:`finally` clause includes a :keyword:`return` +* If a :keyword:`!finally` clause includes a :keyword:`!return` statement, the returned value will be the one from the - :keyword:`finally` clause's :keyword:`return` statement, not the - value from the :keyword:`try` clause's :keyword:`return` statement. + :keyword:`!finally` clause's :keyword:`!return` statement, not the + value from the :keyword:`!try` clause's :keyword:`!return` + statement. For example::