I have this example in book, but it does not work in my python 3.3
x = 'item found'
def search():
raise x or return
try:
search()
except x:
print('exception')
else:
print('no exception')
Could any one tell me why?
Simple: return
is a statement, not an expression. Statements have to appear on their own line. raise
is a statement too, it expects it's expression to evaluate to an exception to raise, but neither x
nor the return
statement fulfills that.
As it stands, the line is complete nonsense. It is not valid Python.
What happens instead, is that the Python parser will flag this code as invalid and raise a SyntaxError
exception for the whole file . No code contained in the file will actually be run:
File "demo.py", line 4
raise x or return
^
SyntaxError: invalid syntax
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.