473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1775
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 thrown and
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 here } catch (Exception ee) {}
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 connection outside of the Try Catch Finally...
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, etc...etc...) Or should Try + Catch be used IN PLACE of...
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 why the computer has to think for 1 second! ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.