471,119 Members | 1,226 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Catch unknown exception

Hello

Python is not my native language, so I'm kinda stuck right now.

I have a small web crawler, that opens pages using urllib.urlopen (1). The
problem is that when a page doesn't exist, I would like to do this and
that. So I have done the following:

import urllib
import socket
socket.setdefaulttimeout(10)

def getPage(self):
try:
self.page = urllib.urlopen(self.link)
self.body = self.page.read()
self.page.close()
except <this is the problem>:
singSongAboutTimeout()

I have tried "except socket.timeout" but this is ignored. The call to
getPage is wrapped in its own try-except:

try:
self.page = getPage(self.link)
self.body = self.page.read()
self.page.close()
except IOError, (errno, errmsg):
# print error

This block catches the exception, but I would really like to catch it
earlier.

How do I do that? Is there a way to check which exception is being caught
- something along the lines of:

try:
# something throws an exception
except:
printHumanReadableDescriptionOfException()

And finally, I have a few problems finding what to write after except...
how can I know what the exception returns (ie. errno and errmsg are
returned by IOError)?

Regards
/Thomas

(1) I have specified a new user-agent as described in
http://python.org/doc/2.3.4/lib/module-urllib.html under _urlopener
Jul 18 '05 #1
1 3599
In article <pa****************************@it-snedkeren.BLACK_HOLE.dk>,
Thomas Lindgaard <th****@it-snedkeren.BLACK_HOLE.dk> wrote:
How do I do that? Is there a way to check which exception is being caught
- something along the lines of:

try:
# something throws an exception
except:
printHumanReadableDescriptionOfException()


You want something like:

import sys
try:
raise IndexError
except:
print sys.exc_info()
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Gary.Hu | last post: by
24 posts views Thread by Steven T. Hatton | last post: by
6 posts views Thread by George | last post: by
11 posts views Thread by Khuong Dinh Pham | last post: by
23 posts views Thread by VB Programmer | last post: by
2 posts views Thread by iftekhar | last post: by
13 posts views Thread by Bit byte | last post: by
3 posts views Thread by joseph | last post: by
3 posts views Thread by lfnovaes | last post: by

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.