try... except with unknown error types

xDog Walker thudfoo at gmail.com
Fri Aug 19 15:22:35 EDT 2011


On Friday 2011 August 19 12:09, Yingjie Lin wrote:
> Hi Python users,
>
> I have been using try...except statements in the situations where I can
> expect a certain type of errors might occur. But  sometimes I don't exactly
> know the possible error types, or sometimes I just can't "spell" the error
> types correctly. For example,
>
> try:
> 	response = urlopen(urljoin(uri1, uri2))
> except urllib2.HTTPError:
> 	print "URL does not exist!"
>
> Though "urllib2.HTTPError" is the error type reported by Python, Python
> doesn't recognize it as an error type name. I tried using "HTTPError" alone
> too, but that's not recognized either.
>
> Does anyone know what error type I should put after the except statement?
> or even better: is there a way not to specify the error types? Thank you.

You probably need to import urllib2 before you can use urllib2.HTTPError.

Otherwise, you can try using the base class:

    except Exception, e:

-- 
I have seen the future and I am not in it.



More information about the Python-list mailing list