views:

32

answers:

1

I want to catch a specific http error and not any one of the entire family.. what I was trying to do is --

import urllib2
try:
   urllib2.urlopen("some url")
except urllib2.HTTPError:
   <whatever>

but what I end up is catching any kind of http error, but I want to catch only if the specified webpage doesn't exist!! probably that's HTTP error 404..but I don't know how to specify that catch only error 404 and let the system run the default handler for other events..ny suggestions??

+2  A: 

Just catch urllib.HTTPError, handle it, and if it's not Error 404, simply use raise to re-raise the exception.

See the Python tutorial.

So you could do:

import urllib2
try:
   urllib2.urlopen("some url")
except urllib2.HTTPError, err:
   if err.code == 404:
       <whatever>
   else:
       raise
Tim Pietzcker
hey!! this works like charm!! i didn't think about the "raise" trick!!thank u so much!!
Arnab Sen Gupta