try... except with unknown error types
Mel
mwilson at the-wire.com
Fri Aug 19 17:22:42 EDT 2011
xDog Walker wrote:
> On Friday 2011 August 19 12:09, Yingjie Lin wrote:
[ ... ]
>> 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:
There are maybe better base classes to use. Running the interpreter:
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib2
>>> help (urllib2.HTTPError)
will show you
class HTTPError(URLError, urllib.addinfourl)
| Raised when HTTP error occurs, but also acts like non-error return
|
| Method resolution order:
| HTTPError
| URLError
| exceptions.IOError
| exceptions.EnvironmentError
| exceptions.StandardError
| exceptions.Exception
| exceptions.BaseException
>
So catching any of urllib2.HTTPError, urllib2.URLError, IOError,
EnvironmentError, or StandardError will detect the exception -- with
increasing levels of generality.
Mel.
More information about the Python-list
mailing list