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: 3.6.5 try/except/else/finally block executes code with typos, no errors
Type: behavior Stage: resolved
Components: Versions: Python 3.6
process
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.

Files
File name Uploaded Description Edit
bug_in_try_exceptions.py 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):
  try:
    print("Hello")
    # 1 + print("Hello")
    except:
      1 + print('Why do you print me?') + 1
    else:
      1 + print('Why do you print me?') + 1
    finally:
      print("finally block")
      return
msg334951 - (view) Author: SilentGhost (SilentGhost) * (Python triager) Date: 2019-02-06 16:26
This seem to be the exactly documented behaviour. From https://docs.python.org/3.8/reference/compound_stmts.html#the-try-statement

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.
History
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