如何获取Python3中的异常详细信息 [英] How to get Exception Detail in Python3

查看:712
本文介绍了如何获取Python3中的异常详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Python3中获取异常的细节



例如在foo.py中

  import sys 

try:
{} .encode('utf8')
除了:
err = sys.exc_info ()[0]

print(本身\,错误)
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是...

 本身< class'AttributeError'> 
.args< attribute'args''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__' ','__setattr__','__sizeof__','__str__','__subclasshook__']
type< class'getset_descriptor'> $ _ $ _ $ _ var{_ __init__':< slot wrapper'__init__''AttributeError'objects>,'__doc__':'Attribute not found。','__new__':<内置方法__new__ 0x821c3a0>}
-------- k,v in vars ---------
__init__
< slot wrapper'__init__'of'AttributeError'objects> ;
__doc__
未找到属性。
__new__
<内置方法__new__类型对象在0x821c3a0>

我想从实例err,$ / b
$ b




  • FILE:foo.py

  • LINE:4

  • MESSAGE:'dict'对象没有属性'encode'



像这个代码的stdout

  {}。encode('utf8')
/ pre>

这个

 追溯(最近最近的电话): 

文件foo.py,第2行在< module>

{} .encode('utf8')

AttributeError:'dict'对象没有属性'encode'
pre>

解决方案

使用 追溯模块从您当前的位置或给定的例外打印追溯。



你没有说明你期望的输出,但是 traceback 模块最有可能产生最适合您需求的输出,无论它们是什么。


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

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 <module>

    {}.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.

这篇关于如何获取Python3中的异常详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆