By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,753 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

are variables local only to try/except blocks?

P: n/a
# Is this valid (or is excp local to try/except)?
try:
try:
doSomething1
excp = 0
except:
excp = 1
#endTry
if (_excp_): doSomething1 # is excp defined here?
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 se****@ca.ibm.com (as well as newsgroup)
Barry Searle, se****@ca.ibm.com

Sep 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
BarrySearle wrote:
# Is this valid
yes.
(or is excp local to try/except)?
no. try/except doesn't introduce a new scope.
try:
try:
doSomething1
excp = 0
except:
excp = 1
#endTry
if (_excp_): doSomething1 # is excp defined here?
yes (but _excp_ isn't defined, so you need to change that if-statement)
excp = 0
except:
excp = 1
#endTry
if (excp): doSomething2 # is excp defined here?


yes.

</F>

Sep 19 '05 #2

P: n/a
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 se****@ca.ibm.com (as well as newsgroup)
Barry Searle, se****@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

Sep 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.