This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: finally block doesn't re-raise exception if return statement exists inside
Type: enhancement Stage: resolved
Components: Documentation Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: David Rebbe, docs@python, iritkatriel, lukasz.langa, miss-islington, pablogsal, robertohueso, serhiy.storchaka
Priority: normal Keywords: easy, patch

Created on 2018-02-11 22:18 by David Rebbe, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
finally_test.py David Rebbe, 2018-02-11 22:18 shows exception handling differences in finally block
Pull Requests
URL Status Linked Edit
PR 25600 merged robertohueso, 2021-04-25 17:31
PR 25890 merged miss-islington, 2021-05-04 12:36
PR 25893 merged miss-islington, 2021-05-04 12:51
Messages (10)
msg312014 - (view) Author: David Rebbe (David Rebbe) Date: 2018-02-11 22:18
According to the docs:
"When an exception has occurred in the try clause and has not been handled by an except clause (or it has occurred in an except or else clause), it is re-raised after the finally clause has been executed."
https://docs.python.org/2/tutorial/errors.html#defining-clean-up-actions

This seems to not be the case if return inside a finally block, the exception needing to be thrown looks like its tossed out.

I'm not sure if this is intended behavior and the docs need to be updated or python isn't doing the correct behavior.



Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
msg312015 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-02-11 22:44
This is intended and documented behavior.

https://docs.python.org/3/reference/compound_stmts.html#the-try-statement
msg312017 - (view) Author: David Rebbe (David Rebbe) Date: 2018-02-11 23:19
Oops I didn't realize I referenced the tutorial documentation.

Maybe it would be better to mention this behavior in the tutorial documentation also.

I've always assumed exception raises take priority over any return/break/continues. Behavior is backwards from what I'd expect but makes sense.
msg313858 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-03-15 08:42
I'm not sure that the tutorial should contain such details. But if you provide a pull request with a short and clear documenting of this behavior, it will be reviewed and may be accepted (not by me, I'm not native English and not tech writer). Otherwise this issue will be closed.
msg313968 - (view) Author: David Rebbe (David Rebbe) Date: 2018-03-16 18:56
I'll have to look into this as I have yet to commit anything but I'll put it on my list of things to do.
msg388690 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-03-14 22:03
The doc has a lot more detail now: https://docs.python.org/3.10/tutorial/errors.html#defining-clean-up-actions

but this point is still not mentioned.
msg391949 - (view) Author: Roberto Hueso (robertohueso) * Date: 2021-04-26 16:52
Just opened PR 25600, if the changes are ok then I can backport it to 3.9 and 3.8 docs.
msg392886 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-05-04 12:50
3.8 is in security fixes mode but I'll happily take this for 3.9.
msg392889 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-05-04 13:17
New changeset 91cb1e20e6a432b8b2c1b5db37b03c84b15384fa by Miss Islington (bot) in branch '3.9':
bpo-32822: Add finally with return/break/continue to the tutorial (GH-25600) (#25890)
https://github.com/python/cpython/commit/91cb1e20e6a432b8b2c1b5db37b03c84b15384fa
msg392890 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-05-04 13:17
New changeset 8655521de8760e8a695bcce92937800c079ea2b2 by Miss Islington (bot) in branch '3.10':
bpo-32822: Add finally with return/break/continue to the tutorial (GH-25600) (#25893)
https://github.com/python/cpython/commit/8655521de8760e8a695bcce92937800c079ea2b2
History
Date User Action Args
2022-04-11 14:58:57adminsetgithub: 77003
2021-07-18 15:30:36pablogsalsetpull_requests: - pull_request25774
2021-07-18 15:26:31pablogsalsetnosy: + pablogsal

pull_requests: + pull_request25774
2021-05-04 13:18:24lukasz.langasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-05-04 13:17:43lukasz.langasetmessages: + msg392890
2021-05-04 13:17:24lukasz.langasetmessages: + msg392889
2021-05-04 12:51:30miss-islingtonsetpull_requests: + pull_request24567
2021-05-04 12:50:35lukasz.langasetnosy: + lukasz.langa

messages: + msg392886
versions: - Python 3.8
2021-05-04 12:36:11miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request24564
2021-04-26 16:52:32robertohuesosetmessages: + msg391949
2021-04-25 17:31:48robertohuesosetkeywords: + patch
nosy: + robertohueso

pull_requests: + pull_request24317
stage: patch review
2021-03-14 22:03:56iritkatrielsetversions: + Python 3.9, Python 3.10, - Python 2.7, Python 3.6, Python 3.7
nosy: + iritkatriel

messages: + msg388690

keywords: + easy
2018-03-16 18:56:03David Rebbesetmessages: + msg313968
2018-03-15 08:42:38serhiy.storchakasetstatus: pending -> open

nosy: + docs@python
versions: + Python 2.7, Python 3.6, Python 3.7, Python 3.8, - Python 3.4
assignee: docs@python
components: + Documentation, - Interpreter Core
type: behavior -> enhancement
2018-03-15 08:42:13serhiy.storchakasetstatus: open -> pending

messages: + msg313858
2018-02-11 23:19:05David Rebbesetmessages: + msg312017
2018-02-11 22:44:33serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg312015
2018-02-11 22:18:17David Rebbecreate