Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5985919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:35:58+00:00 2026-05-22T22:35:58+00:00

I want to catch a urllib2.HTTPError with extra information if it’s a 404: try:

  • 0

I want to catch a urllib2.HTTPError with extra information if it’s a 404:

try:
    data = urlopen(url)
except HTTPError, e:  # Python 2.5 syntax
    if e.code == 404:
        raise HTTPError('data not found on remote')
    else:
        raise

but this doesn’t work because HTTPError‘s init takes multiple arguments, which are undocumented. It it did work, it would lose the backtrace and the original message. I also tried

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

but that just re-raised the exception without extra information. What should I do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:35:58+00:00Added an answer on May 22, 2026 at 10:35 pm

    You just need to use e.msg rather than e.message. The script:

    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
    

    prints

    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
    

    You can of course tidy this up with an enclosing 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
    

    which prints simply

    HTTP Error 404: data not found on remote: Not Found
    

    The exception has all of the original detail: e.__dict__ looks like

    {'__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'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have import urllib2 try: urllib2.urlopen("some url") except urllib2.HTTPError: <whatever> but what I end
In a sort of try/catch form I want to execute a bash that doesn't
The problem drives me to the big try-catch loops. I want smaller. So how
I want to use python urllib2 to simulate a login action, I use Fiddler
I have a situation where I want to catch 404 errors fired by HTML
If I want to catch every single non existing URL that was passed to
I am using codeplex NVelocity library on .net and i want to catch an
I want my app to catch downloads of a particular kind of file. Here's
I want to use VBScript to catch errors and log them (ie on error
I want to catch all exceptions raised (handled or unhandled) to log them. for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.