Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11867 > unrolled thread

Re: try... except with unknown error types

Started byxDog Walker <thudfoo@gmail.com>
First post2011-08-19 12:22 -0700
Last post2011-08-19 17:22 -0400
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: try... except with unknown error types xDog Walker <thudfoo@gmail.com> - 2011-08-19 12:22 -0700
    Re: try... except with unknown error types Mel <mwilson@the-wire.com> - 2011-08-19 17:22 -0400

#11867 — Re: try... except with unknown error types

FromxDog Walker <thudfoo@gmail.com>
Date2011-08-19 12:22 -0700
SubjectRe: try... except with unknown error types
Message-ID<mailman.232.1313781831.27778.python-list@python.org>
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.

[toc] | [next] | [standalone]


#11884

FromMel <mwilson@the-wire.com>
Date2011-08-19 17:22 -0400
Message-ID<j2mk6v$kji$1@speranza.aioe.org>
In reply to#11867
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web