469,609 Members | 1,689 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

How to catch 'error' type exceptions

Hi

I am quite familiar with normal python errors which can
be caught by using the try... except... finally clause. But
very often I find other kinds of exceptions raised in my programs.

Here is an example.

<TRACEBACK>
Traceback (most recent call last):
File "D:\Python22\lib\threading.py", line 414, in __bootstrap
self.run()
File "WebUrlTracker.py", line 213, in run
self.action()
File "WebUrlTracker.py", line 207, in action
self.downloadUrl()
File "WebUrlTracker.py", line 249, in downloadUrl
self.fetchUrl()
File "WebUrlTracker.py", line 319, in fetchUrl
data=self._connection.fetchData(fetchurl)
File "WebUrlConnector.py", line 267, in fetchData
connection.request("GET", relpath)
File "D:\Python22\lib\httplib.py", line 702, in request
self._send_request(method, url, body, headers)
File "D:\Python22\lib\httplib.py", line 724, in _send_request
self.endheaders()
File "D:\Python22\lib\httplib.py", line 696, in endheaders
self._send_output()
File "D:\Python22\lib\httplib.py", line 582, in _send_output
self.send(msg)
File "D:\Python22\lib\httplib.py", line 549, in send
self.connect()
File "D:\Python22\lib\httplib.py", line 789, in connect
error: (10060, 'Operation timed out')
</TRACEBACK>

If I try to catch this using the try... except clause it does not work
(actually it raises another error for trying to catch 'error'), i.e the
following code fails.

try:
<Exception generating code>
except error, e:
print e

Could anyone tell me more about these errors and how to deal with
them ? Probably it is already documented in the python reference, but
I have missed it in that case.

Thanks

Anand Pillai
Jul 18 '05 #1
4 16086
Anand Pillai wrote:
<TRACEBACK>
[ Traceback elided ]
File "D:\Python22\lib\httplib.py", line 789, in connect
error: (10060, 'Operation timed out')
</TRACEBACK>

If I try to catch this using the try... except clause it does not work
(actually it raises another error for trying to catch 'error'), i.e the
following code fails.

try:
<Exception generating code>
except error, e:
print e


import socket

try:
<Exception generating code>
except socket.error, e:
print e

HTH,

--
alan kennedy
-----------------------------------------------------
check http headers here: http://xhaus.com/headers
email alan: http://xhaus.com/mailto/alan
Jul 18 '05 #2
Your problem is that "error" is not a valid exception type in Python.
You "should" be trying to catch a specific problem so that you can
handle it appropriately (such as socket.error as mentioned by another
poster).

The lazy, dangerous way would be:

try:
# some error-generating code
except Exception, reason:
print reason

but that's not recommended good coding practice.

Kevin.

py*******@Hotpop.com (Anand Pillai) wrote in message
news:<84**************************@posting.google. com>...
Hi

I am quite familiar with normal python errors which can
be caught by using the try... except... finally clause. But
very often I find other kinds of exceptions raised in my programs.

Here is an example.

<TRACEBACK>
Traceback (most recent call last):
File "D:\Python22\lib\threading.py", line 414, in __bootstrap
self.run()
File "WebUrlTracker.py", line 213, in run
self.action()
File "WebUrlTracker.py", line 207, in action
self.downloadUrl()
File "WebUrlTracker.py", line 249, in downloadUrl
self.fetchUrl()
File "WebUrlTracker.py", line 319, in fetchUrl
data=self._connection.fetchData(fetchurl)
File "WebUrlConnector.py", line 267, in fetchData
connection.request("GET", relpath)
File "D:\Python22\lib\httplib.py", line 702, in request
self._send_request(method, url, body, headers)
File "D:\Python22\lib\httplib.py", line 724, in _send_request
self.endheaders()
File "D:\Python22\lib\httplib.py", line 696, in endheaders
self._send_output()
File "D:\Python22\lib\httplib.py", line 582, in _send_output
self.send(msg)
File "D:\Python22\lib\httplib.py", line 549, in send
self.connect()
File "D:\Python22\lib\httplib.py", line 789, in connect
error: (10060, 'Operation timed out')
</TRACEBACK>

If I try to catch this using the try... except clause it does not work
(actually it raises another error for trying to catch 'error'), i.e the
following code fails.

try:
<Exception generating code>
except error, e:
print e

Could anyone tell me more about these errors and how to deal with
them ? Probably it is already documented in the python reference, but
I have missed it in that case.

Thanks

Anand Pillai

Jul 18 '05 #3
On Mon, 30 Jun 2003 14:21:58 +0100, Alan Kennedy <al****@hotmail.com> wrote:
Anand Pillai wrote:
<TRACEBACK>


[ Traceback elided ]
File "D:\Python22\lib\httplib.py", line 789, in connect
error: (10060, 'Operation timed out')
</TRACEBACK>

If I try to catch this using the try... except clause it does not work
(actually it raises another error for trying to catch 'error'), i.e the
following code fails.

try:
<Exception generating code>
except error, e:
print e


import socket

try:
<Exception generating code>
except socket.error, e:
print e

HTH,


Sometimes a catchall is desirable, e.g. (untested):

try:
<Exception generating code>
except Exception, e:
print '%s: %s' % (e.__class__.__name__, e)
if isinstance(e, SystemExit): raise # take the exit
except:
print 'Nonstandard Exception %r: %r' % __import__('sys').exc_info()[:2]

HTH2 ;-)

Regards,
Bengt Richter
Jul 18 '05 #4
On 30 Jun 2003 13:22:32 -0700, ke***@cazabon.com (Kevin Cazabon) wrote:
Your problem is that "error" is not a valid exception type in Python.
You "should" be trying to catch a specific problem so that you can
handle it appropriately (such as socket.error as mentioned by another
poster).

The lazy, dangerous way would be:

try:
# some error-generating code
except Exception, reason:
print reason

but that's not recommended good coding practice.

Yes, certainly not internally, unless re-raising all or selected exceptions,
but as an outside wrapper to a whole app, why not? (You could make traceback
printing depend on __debug__ or some other option if desired).

You could also detect and eliminate redundant repetition in a traceback print
of a recursion limit exception. (I think that would be a nice default, BTW).

Regards,
Bengt Richter
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Gary.Hu | last post: by
20 posts views Thread by Tom Groszko | last post: by
11 posts views Thread by kaeli | last post: by
23 posts views Thread by VB Programmer | last post: by
22 posts views Thread by STom | last post: by
9 posts views Thread by Bob Achgill | last post: by
32 posts views Thread by cj | last post: by
6 posts views Thread by rhaazy | last post: by
reply views Thread by Solution2021 | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.