are variables local only to try/except blocks?

Steve Holden steve at holdenweb.com
Tue Sep 20 11:55:13 EDT 2005


BarrySearle wrote:
>  # Is this valid (or is excp local to try/except)?
>  try:
>     try:
>        doSomething1
>        excp = 0

This block is problematic because excp won;t be set if doSomething1 
raises an exception.

>     except:
>        excp = 1
>     #endTry
>     if (_excp_): doSomething1 # is excp defined here?

Presumably you are expecting doSomething1 to fail or succeed in some 
non-deterministic way? Otherwise this will just raise the same exception 
again!

>     excp = 0
>  except:
>     excp = 1
> #endTry
> if (excp): doSomething2 # is excp defined here?
> 
> 
>  # valid, but more verbose (and maybe redundant?)
>  excp = 0
>  try:
>     excp = 0
>     try:
>        doSomething1
>        excp = 0  # reset incase future inner block
>     except:
>        excp = 1
>     #endTry
>     if (_excp_): doSomething1
>     excp = 0  # reset incase inner block set excp=1
>  except:
>     excp = 1
> #endTry
> if (excp): doSomething2
> 
> I am not so interested in what a particular version of the
> Python/Jython interpreter does, but rather what is "right".
> 
> Pls "CC" replies to searle at ca.ibm.com (as well as newsgroup)
> Barry Searle,    searle at ca.ibm.com
> 
Your approach to exception handling is a little simplistic, resulting on 
code that reads about as well as a plate of spaghetti.

What are you actually trying to *do*? What problem do you need to solve?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC                     www.holdenweb.com
PyCon TX 2006                          www.pycon.org




More information about the Python-list mailing list