Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to catch 'error' type exceptions

Reply
Thread Tools

How to catch 'error' type exceptions

 
 
Anand Pillai
Guest
Posts: n/a
 
      06-30-2003
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
 
Reply With Quote
 
 
 
 
Alan Kennedy
Guest
Posts: n/a
 
      06-30-2003
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
 
Reply With Quote
 
 
 
 
Kevin Cazabon
Guest
Posts: n/a
 
      06-30-2003
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.

http://www.velocityreviews.com/forums/(E-Mail Removed) (Anand Pillai) wrote in message
news:<(E-Mail Removed). 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

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-01-2003
On Mon, 30 Jun 2003 14:21:58 +0100, Alan Kennedy <(E-Mail Removed)> 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
 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      07-01-2003
On 30 Jun 2003 13:22:32 -0700, (E-Mail Removed) (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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
catch doesn't catch a thrown exception Marteno Rodia Java 5 08-05-2009 03:30 AM
How do I catch unhandled exceptions Alan Silver ASP .Net 5 03-23-2006 08:51 PM
catch(...) doesn't catch everything Adam C++ 9 02-02-2006 05:02 PM
why catch (...) can not catch such exception John Black C++ 8 08-20-2004 02:34 PM



Advertisments