Python 使用额外信息重新发送HTTPError

Python 使用额外信息重新发送HTTPError,python,exception,exception-handling,urllib2,Python,Exception,Exception Handling,Urllib2,如果是404,我想捕获带有额外信息的urllib2.HTTPError: try: data = urlopen(url) except HTTPError, e: # Python 2.5 syntax if e.code == 404: raise HTTPError('data not found on remote') else: raise 但这不起作用,因为HTTPError的init接受多个未记录的参数。它确实有效,它将丢

如果是404,我想捕获带有额外信息的
urllib2.HTTPError

try:
    data = urlopen(url)
except HTTPError, e:  # Python 2.5 syntax
    if e.code == 404:
        raise HTTPError('data not found on remote')
    else:
        raise
但这不起作用,因为
HTTPError
的init接受多个未记录的参数。它确实有效,它将丢失回溯和原始消息。我也试过了

if e.code == 404:
    e.message = 'data not found on remote: %s' % e.message
raise

但这只是在没有额外信息的情况下重新引发了异常。我该怎么办?

HTTPError已经包含了您需要的所有信息,您可以这样简单地重新发布它

raise HTTPError(e.url, e.code, "your message.", e.hdrs, e.fp)

您只需要使用
e.msg
而不是
e.message
。剧本:

from urllib2 import urlopen, HTTPError

url = 'http://www.red-dove.com/frob'

try:
    data = urlopen(url)
except HTTPError, e:  # Python 2.5 syntax
    if e.code == 404:
        e.msg = 'data not found on remote: %s' % e.msg
    raise
印刷品

Traceback (most recent call last):
  File "c:\temp\test404.py", line 6, in <module>
    data = urlopen(url)
  File "C:\Python\Lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data)
  File "C:\Python\Lib\urllib2.py", line 387, in open
    response = meth(req, response)
  File "C:\Python\Lib\urllib2.py", line 498, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python\Lib\urllib2.py", line 425, in error
    return self._call_chain(*args)
  File "C:\Python\Lib\urllib2.py", line 360, in _call_chain
    result = func(*args)
  File "C:\Python\Lib\urllib2.py", line 506, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: data not found on remote: Not Found
它打印的很简单

HTTP Error 404: data not found on remote: Not Found
该异常包含所有原始细节:
e.\uu dict\uuu
如下所示

{'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at   0x00AF2EF0>>,
 'code': 404,
 'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x00AF2EF0>>,
 'fp': <addinfourl at 12003088 whose fp = <socket._fileobject object at 0x00AF2EF0>>,
 'hdrs': <httplib.HTTPMessage instance at 0x00B727B0>,
 'headers': <httplib.HTTPMessage instance at 0x00B727B0>,
 'msg': 'data not found on remote: Not Found',
 'next': <bound method _fileobject.next of <socket._fileobject object at 0x00AF2EF0>>,
 'read': <bound method _fileobject.read of <socket._fileobject object at 0x00AF2EF0>>,
 'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x00AF2EF0>>,
 'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x00AF2EF0>>,
 'url': 'http://www.red-dove.com/frob'}
{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“代码”:404,
“文件号”:,
“fp”:,
“hdrs”:,
“标题”:,
“msg”:“在远程服务器上找不到数据:找不到”,
“下一步”:,
“阅读”:,
“readline”:,
“阅读行”:,
“url”:”http://www.red-dove.com/frob'}

好的,但现在我仍然丢失了回溯。我如何保存它?当您重新引发异常而不仅仅是
raise
尝试
raisee
时,您能为我回答一个更一般的问题吗?我第一次使用python,当我试图找到HttpError的构造函数参数时,您的答案出现了。您在哪里找到该构造函数的文档?我能找到的唯一官方文件是,但这并没有告诉你任何事情。我注意到很多python文档并没有显示构造函数参数或数据成员,这是我从其他语言中习惯的。我做错了吗?@SteveBroberg你可以在这里找到参数
{'__iter__': <bound method _fileobject.__iter__ of <socket._fileobject object at   0x00AF2EF0>>,
 'code': 404,
 'fileno': <bound method _fileobject.fileno of <socket._fileobject object at 0x00AF2EF0>>,
 'fp': <addinfourl at 12003088 whose fp = <socket._fileobject object at 0x00AF2EF0>>,
 'hdrs': <httplib.HTTPMessage instance at 0x00B727B0>,
 'headers': <httplib.HTTPMessage instance at 0x00B727B0>,
 'msg': 'data not found on remote: Not Found',
 'next': <bound method _fileobject.next of <socket._fileobject object at 0x00AF2EF0>>,
 'read': <bound method _fileobject.read of <socket._fileobject object at 0x00AF2EF0>>,
 'readline': <bound method _fileobject.readline of <socket._fileobject object at 0x00AF2EF0>>,
 'readlines': <bound method _fileobject.readlines of <socket._fileobject object at 0x00AF2EF0>>,
 'url': 'http://www.red-dove.com/frob'}