在python 1.5.2中获取异常类型

如何在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:UsersjrueggDesktoptest.py", line 8, in ?
    b = not_existent_variable
NameError: not_existent_variable
    
已邀请:
它的
except Exception, e:
在Python 1和2中。(虽然
as
也适用于Python 2.6和2.7)。 (为什么你在使用1.5.2!?) 然后获取您使用的错误类型
type(e)
。要在Python 2中获取类型名称,请使用
type(e).__name__
,我不知道它是否适用于1.5.2,您必须检查文档。 更新:没有,但是
e.__class__.__name__
。     

要回复问题请先登录注册