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