Overuse of try/except/else?

J

James Mills

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
 
J

Jean-Michel Pichavant

James said:
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)
 
A

Adam Tauno Williams

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.
 
H

Hans Georg Schaathun

> 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.

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.
 
J

James Mills

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.

cheers
James
 
D

Daniel Kluev

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top