Python 1.5.2で例外のタイプを取得するにはどうすればよいですか?
これを行う:
try:
raise "ABC"
except Exception as e:
print str(e)
SyntaxErrorを与えます:
except Exception as e:
^
SyntaxError: invalid syntax
編集: これは機能しません:
try:
a = 3
b = not_existent_variable
except Exception, e:
print "The error is: " + str(e) + "\n"
a = 3
b = not_existent_variable
実際のエラー(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