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


Groups > comp.lang.python > #11867

Re: try... except with unknown error types

From xDog Walker <thudfoo@gmail.com>
Subject Re: try... except with unknown error types
Date 2011-08-19 12:22 -0700
References <58958843-AE74-44BB-B5E3-96D7A37D779E@mssm.edu>
Newsgroups comp.lang.python
Message-ID <mailman.232.1313781831.27778.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

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

csiph-web