473,320 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 16271
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
20
by: Tom Groszko | last post by:
Given a try catch scenario try { dosomething(); } catch (...) { report some error } Is there any way to figure out what exception is being thrown and
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
6
by: Martin Ortiz | last post by:
Which is best approach? Should Try + Catch be used to only deal with "catastrophic" events (like divide by zero, non-existant file, etc...etc...) Or should Try + Catch be used IN PLACE of...
22
by: STom | last post by:
I heard someone mention to me that the use of try catch exception handling is very expensive (in relative terms of slowing an app down) if it is used frequently. Of course they could not explain...
9
by: Bob Achgill | last post by:
I really like this function but have tried to slow down on using it because I get a 1 second pause each time I use it. I don't really understand why the computer has to think for 1 second! ...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
6
by: rhaazy | last post by:
I am looking for some feedback on using try catch statements. Usually when I start a project I use them for everything, but stop using them as often after the "meat n' potatos" of the project is...
9
by: GiJeet | last post by:
Hello, I come from the VB6 world where we'd put a single ON ERROR GOTO ErrHandler at the top of a method. Now whenever an error happened it would drop into the ErrHandler code. In .Net it seems...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.