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.

Title: 3.6.5 try/except/else/finally block executes code with typos, no errors
Type: behavior Stage: resolved
Components: Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: SilentGhost, dmitry_koshelev
Priority: normal Keywords:

Created on 2019-02-06 16:18 by dmitry_koshelev, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit dmitry_koshelev, 2019-02-06 16:18 An example of having no errors
Messages (2)
msg334950 - (view) Author: DMITRY KOSHELEV (dmitry_koshelev) Date: 2019-02-06 16:18
Hello dear developer!
I was playing with try/else/finally block and have found a bug:

Inside of "else" or/and "except" I can do this 
  1 + print('Why do you print me?') + 1
this would print "Why do you print me?", 
in case if I have "finally" block with a "return" statement, no error raises,
if I don't have finally, nothing is printed.

def foo(var):
    # 1 + print("Hello")
      1 + print('Why do you print me?') + 1
      1 + print('Why do you print me?') + 1
      print("finally block")
msg334951 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-02-06 16:26
This seem to be the exactly documented behaviour. From

If finally is present, it specifies a ‘cleanup’ handler. The try clause is executed, including any except and else clauses. If an exception occurs in any of the clauses and is not handled, the exception is temporarily saved. The finally clause is executed. If there is a saved exception it is re-raised at the end of the finally clause. If the finally clause raises another exception, the saved exception is set as the context of the new exception. If the finally clause executes a return, break or continue statement, the saved exception is discarded.
Date User Action Args
2022-04-11 14:59:10adminsetgithub: 80097
2019-02-06 16:26:22SilentGhostsetstatus: open -> closed

nosy: + SilentGhost
messages: + msg334951

resolution: not a bug
stage: resolved
2019-02-06 16:18:37dmitry_koshelevcreate