I am taking the liberty of copying my response to your off-list reply back to the c.l.py community. (and I don't normally top-post except in politeness to other top-posters :-) Seems to me you could avoid many of your problems by simply re-jigging your template to read doInitialStuff # With presumably no exceptions excp = 0 try: doLotsHere() except aParticularSetOfExceptions: excp = 1 if excp: handleException() doLotsMoreStuff() If you have been experiencing problems with "unbound local variables" this is almost certainly because, as you cast your template, there is the problem that if doLotsHere() raises an exception then the "excp" variable will not be bound. The reason for this is that Python variables (technically names) are only "created" (technically, associated with a value) when they are bound by the execution of an assignment. If the exception is raised before the assignment then the variable still (technically) doesn't exist in the current namespace (which your average Pythonista might describe by saying that the name hasn't been bound). There is *some* static analysis in Python to support name scoping, but names must be bound by assignment in order to be referencable (?) without causing an AttributeError or NameError exception. That's a fundamental part of the way Python works. Hence my suggestion that you assign the zero before you do anything that might raise exceptions. You will also note I suggest you check for specific exceptions (you can check for more that one by using a tuple rather than a single exception), as otherwise you may very well end up treating unanticipated exceptions inappropriately and masking their occurrence. regards Steve Barry Searle wrote: > > Sorry, the quick sample was just to illustrate the problem and solution > template. > The generic template would be: > doInitialStuff() > try: > doLotsHere() > excp = 0 > except: > excp = 1 > #endTry > if (excp): doExceptionHandling() > doLotsMoreStuff() > > The template will be used (is being used) for the automatic generation > of a bunch of Jython code, and I wanted to be sure there were not scope > issues (i.e. that excp was not only defined within the try/except blocks). > > Barry Searle, searle at ca.ibm.com, 905-413-4020 (TL:969-4020) > Barry Searle/Toronto/IBM at IBMCA (D3/639/8200/MKM) > "Architect, WebSphere Tools for WsAdmin Scripting and Automated Build " > > > > *Steve Holden <steve at holdenweb.com>* > > 20/09/2005 11:55 AM > > > To > > cc > Barry Searle/Toronto/IBM at IBMCA > Subject > Re: are variables local only to try/except blocks? > > > > > > > > > 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 > -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb.com PyCon TX 2006 www.pycon.org
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4