are variables local only to try/except blocks?

Discussion in 'Python' started by BarrySearle, Sep 19, 2005.

  1. BarrySearle

    BarrySearle Guest

    # 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 (as well as newsgroup)
    Barry Searle,
     
    BarrySearle, Sep 19, 2005
    #1
    1. Advertising

  2. 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>
     
    Fredrik Lundh, Sep 19, 2005
    #2
    1. Advertising

  3. BarrySearle

    Steve Holden Guest

    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 (as well as newsgroup)
    > Barry Searle,
    >

    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, Sep 20, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Salerno
    Replies:
    20
    Views:
    866
    John Salerno
    Aug 11, 2006
  2. Fabio Z Tessitore

    who is simpler? try/except/else or try/except

    Fabio Z Tessitore, Aug 12, 2007, in forum: Python
    Replies:
    5
    Views:
    384
  3. David House

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    352
    Bruno Desthuilliers
    Jul 6, 2009
  4. matt
    Replies:
    1
    Views:
    284
    George Ogata
    Aug 6, 2004
  5. Christopher Benson-Manica

    Wrap function calls in try/except blocks

    Christopher Benson-Manica, Oct 3, 2005, in forum: Javascript
    Replies:
    7
    Views:
    130
    Richard Cornford
    Oct 7, 2005
Loading...

Share This Page