Title: finally block doesn't re-raise exception if return statement exists inside
Type: enhancement Stage:
Components: Documentation Versions: Python 3.10, Python 3.9, Python 3.8
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: David Rebbe, docs@python, iritkatriel, serhiy.storchaka
Priority: normal Keywords: easy

Created on 2018-02-11 22:18 by David Rebbe, last changed 2021-03-14 22:03 by iritkatriel.

File name Uploaded Description Edit David Rebbe, 2018-02-11 22:18 shows exception handling differences in finally block
Messages (6)
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."

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.
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 triager) Date: 2021-03-14 22:03
The doc has a lot more detail now:

but this point is still not mentioned.
Date User Action Args
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