2

python 2.5 是否允许您传递异常参数?

try: raise Exception("argument here")
except Exception: print Exception.args

我对上面的代码没有运气——我知道你在 Python 2.7 中就是这样做的——这不是在 Python 2.5 中吗?

4

1 回答 1

4

您实际上并没有引发异常,只是创建它。一旦你解决了这个问题,你还需要引用被引发的实例,而不仅仅是 Exception 类:

>>> try: 
...     raise Exception('foo', 23)
... except Exception, e: 
...     print e.args
... 
('foo', 23)
于 2010-10-23T03:51:19.923 回答