如何在Python3中获取异常详细信息
我想获得Python3中异常的详细信息 例如。。。在foo.py中如何在Python3中获取异常详细信息,python,exception,python-3.x,exception-handling,Python,Exception,Python 3.x,Exception Handling,我想获得Python3中异常的详细信息 例如。。。在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
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)
而stdout是
itself <class 'AttributeError'>
.args <attribute 'args' of 'BaseException' objects>
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 <class 'getset_descriptor'>
vars {'__init__': <slot wrapper '__init__' of 'AttributeError' objects>, '__doc__': 'Attribute not found.', '__new__': <built-in method __new__ of type object at 0x821c3a0>}
--------k,v in vars---------
__init__
<slot wrapper '__init__' of 'AttributeError' objects>
__doc__
Attribute not found.
__new__
<built-in method __new__ of type object at 0x821c3a0>
这个
回溯(最近一次呼叫最后一次):
文件“foo.py”,第2行,在
{}.encode('utf8')
AttributeError:“dict”对象没有属性“encode”
使用打印从当前位置或给定异常的回溯
您没有说明您期望的输出是什么,但是
回溯
模块很可能能够生成最适合您需要的输出,无论它们是什么。这正是我想要的!!非常感谢你!我将进一步研究回溯模块。
{}.encode('utf8')
Traceback (most recent call last):
File "foo.py", line 2, in <module>
{}.encode('utf8')
AttributeError: 'dict' object has no attribute 'encode'