我想获取Python3中Exception的详细信息
例如…在foo.py中
import sys
try:
{}.encode('utf8')
except:
err = sys.exc_info()[0]
print("itself\t", err)
print(".args\t", err.args)
print("dir\t", dir(err.args))
print("type\t", type(err.args))
print("vars\t", vars(err))
print("--------k,v in vars---------")
for k,v in vars(err).items():
print(k)
print(v)
和标准输出是…
itself
.args
dir ['__class__', '__delattr__', '__delete__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__get__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__objclass__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
type
vars {'__init__': , '__doc__': 'Attribute not found.', '__new__': }
--------k,v in vars---------
__init__
__doc__
Attribute not found.
__new__
我想从实例“ err”中获得有关此内置异常类的更多信息,
如
文件:foo.py
行:4
讯息:“ dict”对象没有属性“ encode”
就像这段代码的标准输出
{}.encode('utf8')
这个
Traceback (most recent call last):
File "foo.py", line 2, in
{}.encode('utf8')
AttributeError: 'dict' object has no attribute 'encode'