+ 2
Why exclusions do not work?
# Why exclusions do not work? try: print(' "Instruction without errors" ') print(" "foreseeable error" ") except: print(' "If you read this, it means that the error code" ') finally: print(' "Do you understand what I mean?" ') # If do: # print(" "foreseeable error" ") # # It's work: try: print(' "Instruction without errors" ') # print(" "foreseeable error" ") print(666/0) except: print(' "If you read this, it means that the error code" ') finally: print(' "Do you understand what I mean?" ') # "Instruction without errors" # "If you read this, it means that the error code" # "Do you understand what I mean?"
6 Answers
+ 13
To catch syntax errors you need to eval them..
This is bcz the code as a whole is interpreted and it wont be interpreted if it finds a Syntax error
Try this
#Why exclusions do not work?
# Why exclusions do not work?
try:
print(' "Instruction without errors" ')
eval('print(" "foreseeable error" ")')
except:
print(' "If you read this, it means that the error code" ')
finally:
print(' "Do you understand what I mean?" ')
+ 10
Why exclusions do not work?
# Why exclusions do not work?
try:
print(' "Instruction without errors" ')
print(" "foreseeable error" ")
The above print contains invalid syntax i.e proper quotation is needed...
it should be
print("foreseeable error")
As that like contains error.. when u make it a comment , it isn't interpreted and code works for
# print(" " foreseeable error"")
+ 6
It appears you can't catch syntax errors because the code has to be correct for it to run.
+ 1
I made a mistake in the syntax intentionally.
The question is not this ...
The question is why, exceptions do not handle the syntax error, while they work with division by zero.
0
This is part of the code from the training material.
The task was to show an example of the kind of error that can be obtained if there are double quotes in a double quoted string.
print(" That's what will happen if we do this:\n" )
input(""" print(" "Hello WORLD" " )""") # wait for Enter
try:
eval('print(" "Hello WORLD" ")')
except:
print("""
File "<stdin>", line 1
print(" "Hello WORLD" ")
^
SyntaxError: invalid syntax
""")
finally:
print('Do not ever do this:)')
# my
input(""" print(" "Hello WORLD" " )""")
exec('print(" "Hello WORLD" ")')
This code is not so much for teaching anyone, as for consolidating your own knowledge.
0
I don't know