Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overuse of try/except/else?

70 views
Skip to first unread message

Kyle T. Jones

unread,
May 9, 2011, 8:40:02 PM5/9/11
to

It has been hard for me to determine what would constitute overuse.

Cheers.

James Mills

unread,
May 9, 2011, 9:09:01 PM5/9/11
to python list
On Tue, May 10, 2011 at 10:40 AM, Kyle T. Jones
<onexpa...@evomeryahoodotyouknow.com> wrote:
> It has been hard for me to determine what would constitute overuse.

A rule of thumb I always follow and practice is:

"Let the error lie where it occurred."

or

"Don't hide errors.".

It's good practice to follow IHMO as it makes it easier to find
the source of defects in your function(s). If you constantly
do things like try/except/log then it makes finding the source
harder and may make it harder to identify what caused it.

I favor Test Driven Development (TDD) over Contracts in Python
(as Python is a dynamic programming language) but errors should
be handled and caught by the caller - not the callee.

My 2c, others may have other points of view...

cheers
James

--
-- James Mills
--
-- "Problems are solved by method"

Jean-Michel Pichavant

unread,
May 10, 2011, 5:34:10 AM5/10/11
to James Mills, python list
James Mills wrote:
> On Tue, May 10, 2011 at 10:40 AM, Kyle T. Jones
> <onexpa...@evomeryahoodotyouknow.com> wrote:
>
>> It has been hard for me to determine what would constitute overuse.
>>
>
> A rule of thumb I always follow and practice is:
>
> "Let the error lie where it occurred."
>
> or
>
> "Don't hide errors.".
>
> It's good practice to follow IHMO as it makes it easier to find
> the source of defects in your function(s). If you constantly
> do things like try/except/log then it makes finding the source
> harder and may make it harder to identify what caused it.
>
You can reraise the exception without loosing the stack trace.

try:
...
except SomeException, exc:
log(exc)
print 'Hello world'
raise # "raise exc" would loose the original stack trace

JM

PS : "code that crashes could use improvement, but incorrect code that
doesn’t crash is a horrible nightmare." (I don't know the author)

Adam Tauno Williams

unread,
May 10, 2011, 7:36:42 AM5/10/11
to pytho...@python.org
On Mon, 2011-05-09 at 19:40 -0500, Kyle T. Jones wrote:
> It has been hard for me to determine what would constitute overuse.

The chronic problem is under use; so I wouldn't worry much about it.

try/except should occur as often as is required for the application to
either deal gracefully with the condition or report *meaningful* error
messages to the user/administrator.

Hans Georg Schaathun

unread,
May 10, 2011, 8:10:11 AM5/10/11
to
On Tue, 10 May 2011 07:36:42 -0400, Adam Tauno Williams
<awil...@whitemice.org> wrote:

So overuse is really when you cannot do anything meaningful about
the exception. The two interesting questions are really
1. where and when to catch a given exception
2. at what stage of the development cycle catching a particular
(class of) exception should become a priority

There is a potential overuse of exceptions, where they are used for
quite ordinary and frequent (i.e. not exceptional) conditions, and
the information could be passed through the return value instead.
Exceptions is a very flexible, but also rather expensive means of
communications. You can, actually, write any program using raise
instead of return. That would be overuse.

--
:-- Hans Georg

Paul Probert

unread,
May 10, 2011, 11:26:18 PM5/10/11
to
On 05/09/2011 07:40 PM, Kyle T. Jones wrote:
>
> It has been hard for me to determine what would constitute overuse.
>
> Cheers.
Well, for me the power of exceptions is that it lets me write much more
concise code. For example, suppose I call a routine I wrote over and
over, and I have to check for errors on each call. Then you have a long
block of code like:
if err == 0:
x1,err=somefunction(1)
if err == o:
x2,err=somefunction(2)
...
...
but if somefunction just raises an exception on error, then you do
try:
x1=somefunction(1)
x2=somefunction(2)
...
...
except:
blah blah

So for my uses, its handy to let things raise exceptions willy nilly in
the lower level functions, and do the catching in the higher level function.

Paul Probert

James Mills

unread,
May 10, 2011, 11:37:57 PM5/10/11
to python list
On Tue, May 10, 2011 at 7:34 PM, Jean-Michel Pichavant
<jeanm...@sequans.com> wrote:
> You can reraise the exception without loosing the stack trace.
>
> try:
> ...
> except SomeException, exc:
> log(exc)
> print 'Hello world'
> raise # "raise exc" would loose the original stack trace

Valid point :) However I was referring to real experience
where I've seen code that "catches all any any exception"
and simply logs it.

Daniel Kluev

unread,
May 21, 2011, 9:33:33 AM5/21/11
to pytho...@python.org
On Tue, May 10, 2011 at 11:40 AM, Kyle T. Jones
<onexpa...@evomeryahoodotyouknow.com> wrote:
>
> It has been hard for me to determine what would constitute overuse.
>

Good example of abuse is catching KeyboardInterrupt or SystemExit
inside some library code. PycURL does it, and its truly annoying.

--
With best regards,
Daniel Kluev

0 new messages