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

Created on 2018-02-11 22:18 by David Rebbe, last changed 2018-03-16 18:56 by David Rebbe.

Files
File name Uploaded Description Edit
finally_test.py David Rebbe, 2018-02-11 22:18 shows exception handling differences in finally block
Messages (5)
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.
History
Date User Action Args
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