如何获取异常信息? [英] How do I get info on an exception ?
问题描述
使用Python 2.2.2,
我想从socket.gethostbyaddr(ip)中捕获所有异常
来自IDLE,I可以生成:
socket.gethostbyaddr(''1.2'')
<回溯(最近一次调用最后一次):
文件"< pyshell#28>",第1行,在?
套接字中。 gethostbyaddr(''1.2'')
herror:(11004,''找不到主人')< ===当我抓到时我想要什么
当我运行此代码时:
尝试:
主机名,别名,hostip = socket.gethostbyaddr(ip)
return(hostip,主机名)
除外:
print sys.exc_info()
print sys.exc_type
return
print sys.exc_info()给 -
(< class socket.herror at 0x00AE7AC0>,
< socket.herror instance at 0x00C725C0>,
< traceback对象位于0x00C73920>)
和print sys.exc_type给 -
socket.herror
如何获得(11004,''找不到主机'')"部分?
更重要的是,答案记录在哪里我应该看看
?
Frank
Frank写道:
使用Python 2.2.2,
我希望从 socket.gethostbyaddr(ip)"
从IDLE,我可以生成:socket.gethostbyaddr(''1.2'')Traceback (最近一次打电话):
文件"< pyshell#28>",第1行,在?
socket.gethostbyaddr(''1.2'')
herror:(11004 ,''主机未找到'')< ===当我抓住时我想要的东西
当我运行此代码时:
尝试:
hostname,aliases,hostip = socket.gethostbyaddr(ip)
return(hostip,hostname)
除了:
print sys.exc_info()
print sys.exc_type
return
使用格式:
尝试:
...
除了ErrorType之外的
,e:
...用异常对象做一些事情...
import socket
socket.error
< class socket.error在0x8154304>尝试:
.... socket.gethostbyaddr(''1.2'')
....除了socket .error,e:
....打印e,dir(e),e.args
....
(1 ,''未知主持人'')[''__ doc__'',''__ getitem__'',''_ _ _ _ _ _'',''_ _ _ _ _ _ _'''''''''''''''''''''' ''](1,''未知主持人'')
如何获得(11004,''未找到主持人')部分?
更重要的是,答案记录在哪里,我应该看看?
检查异常处理部分。
-
Erik Max Francis&& ma*@alcyone.com && http://www.alcyone.com/max/
__美国加利福尼亚州圣何塞市&& 37 20 N 121 53 W&& & tSftDotIotE
/ \聪明地思考并愚蠢行事是人性。
\ __ / Anatole France
>
>我如何得到(11004,''找不到主持人')" part?更重要的是,答案记录在哪里我应该看看?
查看回溯模块Python标准库。如果您想要打印例外,请执行以下操作:
导入追溯
尝试:< br $>
nosuchobject
除了:
traceback.print_exc()
HTH!
Heiko。
在星期五,2003-07-18在01:33,Frank写道:使用Python 2.2.2,
我想从socket.gethostbyaddr(ip)中捕获所有异常
从IDLE,我可以生成:socket.gethostbyaddr(''1.2'')Traceback(最近一次调用最后一次):
文件"< pyshell#28>",第1行,在?
套接字中.gethostbyaddr(''1.2'')
herror:(11004,''找不到主人''< ===当我抓到我想要的东西时
当我运行这段代码:
尝试:
hostname,aliases,hostip = socket.gethostbyaddr(ip)
return(host ip,hostname)
除外:
print sys.exc_info()
print sys.exc_type
返回
试试:
做特殊的东西......
除了Exception,e:
print e.args,dir(e)
可能该信息仅在e.args中,但您可以在异常中添加其他
属性,其中任何一个都应由dir(e)显示。
例外记录很少,所以我不知道那可能是什么。
注意到。
Ian
Using Python 2.2.2,
I want to catch all exceptions from "socket.gethostbyaddr(ip)"
From IDLE, I can generate:
socket.gethostbyaddr(''1.2'')
Traceback (most recent call last):
File "<pyshell#28>", line 1, in ?
socket.gethostbyaddr(''1.2'')
herror: (11004, ''host not found'') <=== what I want when I catch
When I run this code:
try:
hostname, aliases, hostip = socket.gethostbyaddr(ip)
return (hostip, hostname)
except:
print sys.exc_info()
print sys.exc_type
return
The "print sys.exc_info()" gives-
(<class socket.herror at 0x00AE7AC0>,
<socket.herror instance at 0x00C725C0>,
<traceback object at 0x00C73920>)
And the "print sys.exc_type" gives-
socket.herror
How do I get the "(11004, ''host not found'')" part?
More importantly, where is the answer documented that I should
have looked?
Frank
Frank wrote:
Using Python 2.2.2,
I want to catch all exceptions from "socket.gethostbyaddr(ip)"
From IDLE, I can generate:socket.gethostbyaddr(''1.2'') Traceback (most recent call last):
File "<pyshell#28>", line 1, in ?
socket.gethostbyaddr(''1.2'')
herror: (11004, ''host not found'') <=== what I want when I catch
When I run this code:
try:
hostname, aliases, hostip = socket.gethostbyaddr(ip)
return (hostip, hostname)
except:
print sys.exc_info()
print sys.exc_type
return
Use the format:
try:
...
except ErrorType, e:
... do something with exception object e ...
import socket
socket.error <class socket.error at 0x8154304> try:
.... socket.gethostbyaddr(''1.2'')
.... except socket.error, e:
.... print e, dir(e), e.args
....
(1, ''Unknown host'') [''__doc__'', ''__getitem__'', ''__init__'', ''__module__'',
''__str__'', ''args''] (1, ''Unknown host'')
How do I get the "(11004, ''host not found'')" part?
More importantly, where is the answer documented that I should
have looked?
Check the part on exception handling.
--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ It is human nature to think wisely and act foolishly.
\__/ Anatole France
> How do I get the "(11004, ''host not found'')" part?More importantly, where is the answer documented that I should
have looked?
Look at the traceback module from the Python standard library. If you
just wish to print the exception, do the following:
import traceback
try:
nosuchobject
except:
traceback.print_exc()
HTH!
Heiko.
On Fri, 2003-07-18 at 01:33, Frank wrote:Using Python 2.2.2,
I want to catch all exceptions from "socket.gethostbyaddr(ip)"From IDLE, I can generate:socket.gethostbyaddr(''1.2'')Traceback (most recent call last):
File "<pyshell#28>", line 1, in ?
socket.gethostbyaddr(''1.2'')
herror: (11004, ''host not found'') <=== what I want when I catch
When I run this code:
try:
hostname, aliases, hostip = socket.gethostbyaddr(ip)
return (hostip, hostname)
except:
print sys.exc_info()
print sys.exc_type
return
try:
do exceptional stuff...
except Exception, e:
print e.args, dir(e)
Probably that information is just in e.args, but you can add other
attributes to exceptions, any of which should be revealed by dir(e).
Exceptions are poorly documented, so I don''t know where that might be
noted.
Ian
这篇关于如何获取异常信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!