python3异常,如何获取Python3中的异常详细信息

I want to get details of Exception in Python3

for example... in 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)

and stdout is...

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__

I want more information of this built in Exception class from instance "err",

such as

FILE : foo.py

LINE : 4

MESSAGE : 'dict' object has no attribute 'encode'

just like stdout of this code

{}.encode('utf8')

this

Traceback (most recent call last):

File "foo.py", line 2, in

{}.encode('utf8')

AttributeError: 'dict' object has no attribute 'encode'

解决方案

Use the traceback module to print tracebacks from your current position or for a given exception.

You don't state what output you are expecting, but the traceback module most likely will be able to produce output best suited for your needs, whatever they may be.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值