How can I get the type of an Exception in python 1.5.2?
doing this:
try:
raise "ABC"
except Exception as e:
print str(e)
gives an SyntaxError:
except Exception as e:
^
SyntaxError: invalid syntax
EDIT: this does not work:
try:
a = 3
b = not_existent_variable
except Exception, e:
print "The error is: " + str(e) + "\n"
a = 3
b = not_existent_variable
as I only get the argument, not the actual error (NameError):
The error is: not_existent_variable
Traceback (innermost last):
File "C:\Users\jruegg\Desktop\test.py", line 8, in ?
b = not_existent_variable
NameError: not_existent_variable
It's
except Exception, e:
In Python 1 and 2. (Although as
also works in Python 2.6 and 2.7).
(Why on earth are you using 1.5.2!?)
To then get the type of the error you use type(e)
. To get the type name in Python 2 you use type(e).__name__
, I have no idea if that works in 1.5.2, you'll have to check the docs.
Update: It didn't, but e.__class__.__name__
did.