Overuse of try/except/else?

Paul Probert paulprobert at sbcglobal.net
Tue May 10 23:26:18 EDT 2011


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



More information about the Python-list mailing list