473,796 Members | 2,603 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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\li b\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.downloadUr l()
File "WebUrlTracker. py", line 249, in downloadUrl
self.fetchUrl()
File "WebUrlTracker. py", line 319, in fetchUrl
data=self._conn ection.fetchDat a(fetchurl)
File "WebUrlConnecto r.py", line 267, in fetchData
connection.requ est("GET", relpath)
File "D:\Python22\li b\httplib.py", line 702, in request
self._send_requ est(method, url, body, headers)
File "D:\Python22\li b\httplib.py", line 724, in _send_request
self.endheaders ()
File "D:\Python22\li b\httplib.py", line 696, in endheaders
self._send_outp ut()
File "D:\Python22\li b\httplib.py", line 582, in _send_output
self.send(msg)
File "D:\Python22\li b\httplib.py", line 549, in send
self.connect()
File "D:\Python22\li b\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 16300
Anand Pillai wrote:
<TRACEBACK>
[ Traceback elided ]
File "D:\Python22\li b\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*******@Hotpo p.com (Anand Pillai) wrote in message
news:<84******* *************** ****@posting.go ogle.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\li b\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.downloadUr l()
File "WebUrlTracker. py", line 249, in downloadUrl
self.fetchUrl()
File "WebUrlTracker. py", line 319, in fetchUrl
data=self._conn ection.fetchDat a(fetchurl)
File "WebUrlConnecto r.py", line 267, in fetchData
connection.requ est("GET", relpath)
File "D:\Python22\li b\httplib.py", line 702, in request
self._send_requ est(method, url, body, headers)
File "D:\Python22\li b\httplib.py", line 724, in _send_request
self.endheaders ()
File "D:\Python22\li b\httplib.py", line 696, in endheaders
self._send_outp ut()
File "D:\Python22\li b\httplib.py", line 582, in _send_output
self.send(msg)
File "D:\Python22\li b\httplib.py", line 549, in send
self.connect()
File "D:\Python22\li b\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\li b\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.c om (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
30324
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, it quitted with core dumped. %test
20
674
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
6895
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 non-supporting browsers? TIA -- --
23
3083
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
6
5052
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 regular defensive programming? (ie if file exists do this, if not do something else) Or should Try + Catch be used TO SUPPLAMENT regular defensive programming?
22
3376
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 why. Is this true? If so, why? Thanks. STom
9
1652
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! Especially at 2.8 GZ and 1GB RAM. The same pause happens on different situations: database access Trys, Array access Trys. Hummm??
32
6130
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 understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
6
1529
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 finished. I am thinking it would be useful to at least have a blanket try-catch to surround all of the code, and add more to aid in debugging and catching specific exceptions. I want to change my habits but before I do I wanted to see if anyone...
9
2810
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 like you have too many Try/Catch statements all throughout the code. I think this is ugly error handling. Is there a better way to handle exceptions in .Net 2.0 + then putting all these Try/Catch blocks around code? G
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9535
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7558
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5453
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4127
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.