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: print "Hi" in python 3 exception handling doesn't work
Type: behavior Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Mr.Potato1029, abarry, r.david.murray, zach.ware
Priority: normal Keywords:

Created on 2015-12-07 22:44 by Mr.Potato1029, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg256083 - (view) Author: Potato33556611 (Mr.Potato1029) Date: 2015-12-07 22:44
When you execute the code:
try:
    print "Hi"
except:
    print("Hello")

in python 3.5, it creates a syntax error in Terminal on Mac and a pop-up error in IDLE, while it should just print Hello in the console.
msg256084 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2015-12-07 22:53
The reason you are experiencing this behviour is because of the way Python works. Python needs to compile your code before it can execute it. It parses the code, sees an invalid token ('print "Hi"'), fails to compile and throws an error. Your code never gets executed because Python is not able to compile it in the first place. Try adding a print call before the try-except block and you'll see it never gets executed either :)
msg256091 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2015-12-07 23:44
By the way, if your goal is to write python2/3 compatible code, notice that 'print("hello")' is valid in python2 and will do the same thing as print "hello"...as long as you don't use commas in the argument list to print.
msg256101 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2015-12-08 02:46
Also, if you only need to support Python 2.6+, you can use 'from __future__ import print_function' and get all the benefits of 'print' as a function in Python 2 (except the 'flush' argument, which was added in Python 3.3).
History
Date User Action Args
2022-04-11 14:58:24adminsetgithub: 70005
2015-12-08 02:46:51zach.waresetnosy: + zach.ware
messages: + msg256101
2015-12-07 23:44:50r.david.murraysetnosy: + r.david.murray
messages: + msg256091
2015-12-07 22:53:50abarrysetstatus: open -> closed

nosy: + abarry
messages: + msg256084

resolution: not a bug
stage: resolved
2015-12-07 22:44:49Mr.Potato1029create