How to catch 'error' type exceptions

A

Anand Pillai

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
 
A

Alan Kennedy

Anand said:
<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,
 
K

Kevin Cazabon

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.

(e-mail address removed) (Anand Pillai) wrote in message
 
B

Bengt Richter

Anand said:
<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
 
B

Bengt Richter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top