用额外的信息提高HTTPError

问题描述

| 如果它是404,我想用一个额外的信息捕获一个:0ѭ:
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
但这只是在没有额外信息的情况下重新引发了异常。我该怎么办?     

解决方法

        您只需要使用
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(),hdrs,fp)
urllib2.HTTPError: HTTP Error 404: data not found on remote: Not Found
您当然可以使用随附的try / except进行整理:
from urllib2 import urlopen,HTTPError

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

try:
    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
except HTTPError,e:
    print e
简单地打印
HTTP Error 404: data not found on remote: Not Found
该异常具有所有原始细节:
e.__dict__
看起来像
{\'__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\'}
    ,        HTTPError已经包含了您需要的所有信息,您可以像这样简单地重新提出它
raise HTTPError(e.url,e.code,\"your message.\",e.hdrs,e.fp)
    

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...