472,342 Members | 1,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

How to Catch 2 Exceptions at once?

How can I catch 2 exceptions at once for example:

try:
self.gses = opener.open(req)
except (urllib2.HTTPError,urllib2.URLError):
do something..

Seems to work, but how do I also get information about the error?

--
Gregory Piñero
Chief Innovation Officer
Blended Technologies
(www.blendedtechnologies.com)
Oct 1 '06 #1
2 1727
Gregory Piñero wrote:
How can I catch 2 exceptions at once for example:

try:
self.gses = opener.open(req)
except (urllib2.HTTPError,urllib2.URLError):
do something..

Seems to work, but how do I also get information about the error?
pytry:
.... raise ValueError, 'Illegal value for your shoe size!'
.... except (IndexError, ValueError), e:
.... print e
....
Illegal value for your shoe size!
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Oct 1 '06 #2

Gregory Piñero wrote:
How can I catch 2 exceptions at once for example:

try:
self.gses = opener.open(req)
except (urllib2.HTTPError,urllib2.URLError):
do something..

Seems to work, but how do I also get information about the error?
Errr .. the same way as if you mentioned only one exception. The
following is an expansion of the scarcely-describable-as-bare coverage
in the tutorial
(http://docs.python.org/tut/node10.ht...000000000000):

C:\junk>cat gregpexc.py
import sys
try:
fname = raw_input('File name:')
f = open(fname)
i = 1 / 0
# except IOError, (errno, strerror):
# print "I/O error(%s): %s" % (errno, strerror)
except (IOError, KeyboardInterrupt, ZeroDivisionError), e :
print repr(e)
print e
print dir(e)
print e.args
print e.__class__.__name__
except:
print "Unexpected error:", sys.exc_info()[0]
# no example shown; read the fine manual :-)
raise

C:\junk>python gregpexc.py
File name:<exceptions.KeyboardInterrupt instance at 0x00AF1EE0>

['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
()
KeyboardInterrupt

C:\junk>python gregpexc.py
File name:kl;lklklklk
<exceptions.IOError instance at 0x00AF1F08>
[Errno 2] No such file or directory: 'kl;lklklklk'
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args',
'errno',
'filename', 'strerror']
(2, 'No such file or directory')
IOError

C:\junk>python gregpexc.py
File name:gregpexc.py
<exceptions.ZeroDivisionError instance at 0x00AF1F08>
integer division or modulo by zero
['__doc__', '__getitem__', '__init__', '__module__', '__str__', 'args']
('integer division or modulo by zero',)
ZeroDivisionError

HTH,
John

Oct 1 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Tom Groszko | last post by:
Given a try catch scenario try { dosomething(); } catch (...) { report some error } Is there any way to figure out what exception is being...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the...
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file,...
9
by: Bob Achgill | last post by:
I really like this function but have tried to slow down on using it because I get a 1 second pause each time I use it. I don't really understand...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.