Fully handling exceptions.
Skip Montanaro
skip at pobox.com
Sat Aug 18 17:38:30 EDT 2001
Jordan> try:
Jordan> src_in = urllib.urlopen(ag_url)
Jordan> except IOError:
Jordan> print '%s : connection cannot be made or \
Jordan> server returned error.' %(ag_url)
Jordan> except:
Jordan> print '%s : unknown error.' %(ag_url)
Jordan> urllib's manual documents IOError as the only possible
Jordan> exception raised by urlopen; does this make my last except
Jordan> superfluous, or can there be other exceptions raised when
Jordan> urlopen is called that the last except may handle?
If you happen to hit Ctrl-C while urllib.urlopen is executing, you can get a
KeyboardInterrupt. That's not strictly speaking something that urllib can
catch, but probably an interrupt you don't want to catch quite the way you
do. How about
try:
src_in = urllib.urlopen(ag_url)
except IOError:
print '...'
except KeyboardInterrupt:
raise
except:
print '%s : unknown error.' %(ag_url)
Also, if you do catch an unexpected exception, you might well want to
display it. See the docs for sys.get_exc_info().
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list