Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python 1.5.2中获取异常类型_Python_Exception_Syntax Error_Raise - Fatal编程技术网

在python 1.5.2中获取异常类型

在python 1.5.2中获取异常类型,python,exception,syntax-error,raise,Python,Exception,Syntax Error,Raise,如何在python 1.5.2中获取异常类型 这样做: try: raise "ABC" except Exception as e: print str(e) 给出一个语法错误: except Exception as e: ^ SyntaxError: invalid syntax 编辑: 这不起作用: try: a = 3 b = not_existent_variable except Exception,

如何在python 1.5.2中获取异常类型

这样做:

try:
    raise "ABC"
except Exception as e:
    print str(e)
给出一个语法错误:

  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):

它是

在Python1和Python2中。(虽然
as
也适用于Python 2.6和2.7)

(你到底为什么要用1.5.2!)

然后使用
type(e)
获取错误类型。要在Python2中获得类型名,您需要使用
type(e)。\uuuuu name\uuuuuu
,我不知道这在1.5.2中是否有效,您必须检查文档


更新:它没有,但是
e.\uuuuu class\uuuuuu.\uuuuuu name\uuuuuuuuuu.\uuuuuuuuuuuuuu.\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu name.

也许他在。。。我的支持票现在正把你推过10公里的障碍——祝贺你!明智地使用你的新能力,年轻的学徒。:)像这样的事。。。我正在开发telit gsm模块(),1.5.2在硬件方面。但是,您的解决方案似乎不起作用。。。(见我的编辑)@Jan Rüegg:更新了如何获取类型。str(e)将其转换为字符串,而这不包括该异常的类型名。啊,对了,当然,这是1.5.2中的一个老式类。请尝试
e.\uuuuuu class.\uuuuuu name.\uuuuuu
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
except Exception, e: