Consider the following code:
try:
if True a = 1 #It's missing a colon So it's a SyntaxError!!!!!!!
except SyntaxError:
print 'hey'
You'd expect it to print hey
However It raises a SyntaxError
, The same error I'm trying to avoid. So Can all Exceptions be handled using a try-except
block? Well If SyntaxError
's were an exception why is it included in the built-in exceptions? and finally how can I fix the above piece of code so that it handles the exception properly?
Note: I know what I'm trying to do Is utterly pointless and serves no real purpose
See Question&Answers more detail:os