Re: How to except the unexpected?

2006-03-05 Thread Rene Pijlman
Steven D'Aprano: The OP is doing it because catching all exceptions masks bugs. There are certain exceptions which should be allowed through, as they indicate a bug in the OP's code. Normally the tactic is to catch only the exceptions you are interested in, and let everything else through, but the

Re: How to except the unexpected?

2006-03-05 Thread Scott David Daniels
Rene Pijlman wrote: Steven D'Aprano: The OP is doing it because catching all exceptions masks bugs. There are certain exceptions which should be allowed through, as they indicate a bug in the OP's code. Normally the tactic is to catch only the exceptions you are interested in, and let

Re: How to except the unexpected?

2006-03-05 Thread plahey
Yes, and that's the Right Thing(tm) to do. Source code don't lie. Source code don't get out of sync. So source code *is* the best documentation (or at least the most accurate). I could not disagree more strongly with this. Not just no, but hell no! Yes, and that's the Right Thing(tm) to do.

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Roy Smith: I like to create a top-level exception class to encompass all the possible errors in a given module, then subclass that. This way, if you want to catch anything to goes wrong in a call, you can catch the top-level exception class without having to enumerate them all. What do you

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
James Stroud: Which suggests that try: except HTTPException: will be specific enough as a catchall for this module. The following, then, should catch everything you mentioned except the socket timeout: Your conclusion may be (almost) right in this case. I just don't like this approach.

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Peter Hansen: Good code should probably have a very small set of real exception handling cases, and one or two catchalls at a higher level to avoid barfing a traceback at the user. Good point. A catchall seems like a bad idea, since it also catches AttributeErrors and other bugs in the

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Paul Rubin http://[EMAIL PROTECTED]: We have to get Knuth using Python. Perhaps a MIX emulator and running TeXDoctest on his books will convince him.. -- René Pijlman -- http://mail.python.org/mailman/listinfo/python-list

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Steven D'Aprano: ExpectedErrors = (URLError, IOError) ErrorsThatCantHappen = try: process_things() except ExpectedErrors: recover_from_error_gracefully() except ErrorsThatCantHappen: print Congratulations! You have found a program bug! print For a $327.68 reward, please send the

Re: How to except the unexpected?

2006-03-04 Thread Jorge Godoy
Rene Pijlman [EMAIL PROTECTED] writes: With low coverage, yes. But unit testing isn't the answer for this particular problem. For example, yesterday my app was surprised by an httplib.InvalidURL since I hadn't noticed this could be raised by robotparser (this is undocumented). If that fact

Re: How to except the unexpected?

2006-03-04 Thread Roy Smith
Rene Pijlman [EMAIL PROTECTED] wrote: A catchall seems like a bad idea, since it also catches AttributeErrors and other bugs in the program. All of the things like AttributeError are subclasses of StandardError. You can catch those first, and then catch everything else. In theory, all

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Jorge Godoy: Rene Pijlman: my app was surprised by an httplib.InvalidURL since I hadn't noticed this could be raised by robotparser (this is undocumented). It isn't undocumented in my module. From 'pydoc httplib': That's cheating: pydoc is reading the source :-) What I meant was, I'm

Re: How to except the unexpected?

2006-03-04 Thread Rene Pijlman
Roy Smith: In theory, all exceptions which represent problems with the external environment (rather than programming mistakes) should derive from Exception, but not from StandardError. Are you sure? The class hierarchy for built-in exceptions is: Exception +-- StandardError |

Re: How to except the unexpected?

2006-03-04 Thread Roy Smith
In article [EMAIL PROTECTED], Rene Pijlman [EMAIL PROTECTED] wrote: Roy Smith: In theory, all exceptions which represent problems with the external environment (rather than programming mistakes) should derive from Exception, but not from StandardError. Are you sure? The class

Re: How to except the unexpected?

2006-03-04 Thread Roman Susi
Rene Pijlman wrote: Paul Rubin http://[EMAIL PROTECTED]: We have to get Knuth using Python. Perhaps a MIX emulator and running TeXDoctest on his books will convince him.. Or maybe Python written for MIX... -Roman -- http://mail.python.org/mailman/listinfo/python-list

Re: How to except the unexpected?

2006-03-04 Thread Bruno Desthuilliers
Rene Pijlman a écrit : Jorge Godoy: Rene Pijlman: my app was surprised by an httplib.InvalidURL since I hadn't noticed this could be raised by robotparser (this is undocumented). It isn't undocumented in my module. From 'pydoc httplib': That's cheating: pydoc is reading the source :-)

Re: How to except the unexpected?

2006-03-04 Thread James Stroud
Rene Pijlman wrote: Steven D'Aprano: ExpectedErrors = (URLError, IOError) ErrorsThatCantHappen = try: process_things() except ExpectedErrors: recover_from_error_gracefully() except ErrorsThatCantHappen: print Congratulations! You have found a program bug! print For a $327.68

Re: How to except the unexpected?

2006-03-04 Thread Alex Martelli
James Stroud [EMAIL PROTECTED] wrote: Reraise = (LookupError, ArithmeticError, AssertionError) # And then some try: process_things() except Reraise: raise except: log_error() Why catch an error only to re-raise it? To avoid the following handler[s] -- a pretty

Re: How to except the unexpected?

2006-03-04 Thread Paul Rubin
James Stroud [EMAIL PROTECTED] writes: try: process_things() except Reraise: raise except: log_error() Why catch an error only to re-raise it? This falls under http://c2.com/cgi/wiki?YouReallyArentGonnaNeedThis No, without the reraise, the bare except: clause would

Re: How to except the unexpected?

2006-03-04 Thread James Stroud
Rene Pijlman wrote: James Stroud: Which suggests that try: except HTTPException: will be specific enough as a catchall for this module. The following, then, should catch everything you mentioned except the socket timeout: Your conclusion may be (almost) right in this case. I just don't

Re: How to except the unexpected?

2006-03-04 Thread Paul Rubin
James Stroud [EMAIL PROTECTED] writes: approach. Basically this is reverse engineering the interface from the source at the time of writing the app. This is using the source as documentation, there is no law against that. That's completely bogus. Undocumented interfaces in the library

Re: How to except the unexpected?

2006-03-04 Thread James Stroud
Paul Rubin wrote: James Stroud [EMAIL PROTECTED] writes: approach. Basically this is reverse engineering the interface from the source at the time of writing the app. This is using the source as documentation, there is no law against that. That's completely bogus. Undocumented interfaces

Re: How to except the unexpected?

2006-03-04 Thread Paul Rubin
James Stroud [EMAIL PROTECTED] writes: My suggestion was to use some common sense about the source code and apply it. The common sense to apply is that if the source code says one thing and the documentation says another, then one of them is wrong and should be updated. Usually it's the source

Re: How to except the unexpected?

2006-03-04 Thread Steven D'Aprano
On Sat, 04 Mar 2006 13:19:29 -0800, James Stroud wrote: Why catch an error only to re-raise it? As a general tactic, you may want to catch an exception, print extra debugging information, then raise the exception again, like this: try: x = somedata() y = someotherdata() z =

How to except the unexpected?

2006-03-03 Thread Rene Pijlman
One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses urllib2, urlparse, robotparser and some other modules in the battery pack. One day my app failed with an

Re: How to except the unexpected?

2006-03-03 Thread James Stroud
Rene Pijlman wrote: One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses urllib2, urlparse, robotparser and some other modules in the battery pack. One

Re: How to except the unexpected?

2006-03-03 Thread Ben Caradoc-Davies
James Stroud wrote: except URLError, HTTPException: Aieee! This catches only URLError and binds the name HTTPException to the detail of that error. You must write except (URLError, HTTPException): to catch both. -- Ben Caradoc-Davies [EMAIL PROTECTED] http://wintersun.org/ Those who deny

Re: How to except the unexpected?

2006-03-03 Thread James Stroud
Ben Caradoc-Davies wrote: James Stroud wrote: except URLError, HTTPException: Aieee! This catches only URLError and binds the name HTTPException to the detail of that error. You must write except (URLError, HTTPException): to catch both. Oops. --

Re: How to except the unexpected?

2006-03-03 Thread Roy Smith
In article [EMAIL PROTECTED], Ben Caradoc-Davies [EMAIL PROTECTED] wrote: James Stroud wrote: except URLError, HTTPException: Aieee! This catches only URLError and binds the name HTTPException to the detail of that error. You must write except (URLError, HTTPException): to catch

Re: How to except the unexpected?

2006-03-03 Thread Peter Hansen
Rene Pijlman wrote: One of the things I dislike about Java is the need to declare exceptions as part of an interface or class definition. But perhaps Java got this right... I've writen an application that uses urllib2, urlparse, robotparser and some other modules in the battery pack. One

Re: How to except the unexpected?

2006-03-03 Thread Steven D'Aprano
On Sat, 04 Mar 2006 00:10:17 +0100, Rene Pijlman wrote: I've writen an application that uses urllib2, urlparse, robotparser and some other modules in the battery pack. One day my app failed with an urllib2.HTTPError. So I catch that. But then I get a urllib2.URLError, so I catch that too. The

Re: How to except the unexpected?

2006-03-03 Thread Paul Rubin
Steven D'Aprano [EMAIL PROTECTED] writes: try: process_things() except ExpectedErrors: recover_from_error_gracefully() except ErrorsThatCantHappen: print Congratulations! You have found a program bug! print For a $327.68 reward, please send the following \ traceback to

Re: How to except the unexpected?

2006-03-03 Thread Steven D'Aprano
On Fri, 03 Mar 2006 21:10:22 -0800, Paul Rubin wrote: Steven D'Aprano [EMAIL PROTECTED] writes: try: process_things() except ExpectedErrors: recover_from_error_gracefully() except ErrorsThatCantHappen: print Congratulations! You have found a program bug! print For a

Re: How to except the unexpected?

2006-03-03 Thread Paul Rubin
Steven D'Aprano [EMAIL PROTECTED] writes: The way to deal with it is to add another except clause to deal with the KeyboardInterrupt, or to have recover_from_error_gracefully() deal with it. I think adding another except clause for KeyboardInterrupt isn't good because maybe in Python 2.6 or