coding style - try, except

Discussion in 'Python' started by RGK, Feb 25, 2009.

  1. RGK

    RGK Guest

    I'm still learning, so eager to see if there is some community wisdom
    about use of the try/except structures in this situation.

    I find myself with some potentially risky stuff and wrap it in a
    try/except structure with good functional results, though my code leaves
    me a bit uneasy. Maybe it's just esoteric, but your input is appreciated.

    Consider

    try:
    do something 1
    do something 2
    do something 3
    do something 4
    ...
    do something 25

    except:
    print "Oops something didn't work"


    The risky things are just 1 & 2, and the others are not of concern, but
    are dependent on 1 & 2. The alternative is to do:

    wentOkay = True
    try:
    do something 1
    do something 2

    except:
    print "Oops something didn't work"
    wentOkay = False

    if wentOkay:
    do something 3
    do something 4
    ...
    do something 25


    Which seems a bit verbose, but likely the better approach. Is there
    some other option I should be considering?

    Any input appreciated :)

    Ross.
    RGK, Feb 25, 2009
    #1
    1. Advertising

  2. RGK

    Steve Holden Guest

    RGK wrote:
    >
    > I'm still learning, so eager to see if there is some community wisdom
    > about use of the try/except structures in this situation.
    >
    > I find myself with some potentially risky stuff and wrap it in a
    > try/except structure with good functional results, though my code leaves
    > me a bit uneasy. Maybe it's just esoteric, but your input is appreciated.
    >
    > Consider
    >
    > try:
    > do something 1
    > do something 2
    > do something 3
    > do something 4
    > ...
    > do something 25
    >
    > except:
    > print "Oops something didn't work"
    >
    >
    > The risky things are just 1 & 2, and the others are not of concern, but
    > are dependent on 1 & 2. The alternative is to do:
    >
    > wentOkay = True
    > try:
    > do something 1
    > do something 2
    >
    > except:
    > print "Oops something didn't work"
    > wentOkay = False
    >
    > if wentOkay:
    > do something 3
    > do something 4
    > ...
    > do something 25
    >
    >
    > Which seems a bit verbose, but likely the better approach. Is there
    > some other option I should be considering?
    >
    > Any input appreciated :)
    >

    The first form is far preferable: it expresses the logic directly and
    clearly, and is much easier to read than the second, which I personally
    find somewhat contorted.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Feb 25, 2009
    #2
    1. Advertising

  3. RGK

    Peter Otten Guest

    Steve Holden wrote:

    > RGK wrote:
    >>
    >> I'm still learning, so eager to see if there is some community wisdom
    >> about use of the try/except structures in this situation.
    >>
    >> I find myself with some potentially risky stuff and wrap it in a
    >> try/except structure with good functional results, though my code leaves
    >> me a bit uneasy. Maybe it's just esoteric, but your input is appreciated.
    >>
    >> Consider
    >>
    >> try:
    >> do something 1
    >> do something 2
    >> do something 3
    >> do something 4
    >> ...
    >> do something 25
    >>
    >> except:
    >> print "Oops something didn't work"


    If you don't want a specific treatment for errors anticipated in 1 and 2
    there's no need for try...except at this level at all. Just pass control up
    the stack.

    >> The risky things are just 1 & 2, and the others are not of concern, but
    >> are dependent on 1 & 2. The alternative is to do:
    >>
    >> wentOkay = True
    >> try:
    >> do something 1
    >> do something 2
    >>
    >> except:
    >> print "Oops something didn't work"
    >> wentOkay = False
    >>
    >> if wentOkay:
    >> do something 3
    >> do something 4
    >> ...
    >> do something 25
    >>
    >>
    >> Which seems a bit verbose, but likely the better approach. Is there
    >> some other option I should be considering?
    >>
    >> Any input appreciated :)
    >>

    > The first form is far preferable: it expresses the logic directly and
    > clearly, and is much easier to read than the second, which I personally
    > find somewhat contorted.


    How about

    try:
    # do something that may fail in a way you anticipate
    do something 1
    do something 2
    except SpecificError:
    deal with the problem or reraise
    else:
    # no errors above
    do something 3...25

    Peter
    Peter Otten, Feb 25, 2009
    #3
  4. RGK

    Chris Rebert Guest

    On Wed, Feb 25, 2009 at 9:36 AM, RGK <> wrote:
    >
    > I'm still learning, so eager to see if there is some community wisdom about
    > use of the try/except structures in this situation.
    >
    > I find myself with some potentially risky stuff and wrap it in a try/except
    > structure with good functional results, though my code leaves me a bit
    > uneasy. Maybe it's just esoteric, but your input is appreciated.
    >
    > Consider
    >
    >  try:
    >    do something 1
    >    do something 2
    >    do something 3
    >    do something 4
    >    ...
    >    do something 25
    >
    >  except:
    >    print "Oops something didn't work"
    >
    >
    > The risky things are just 1 & 2, and the others are not of concern, but are
    > dependent on 1 & 2.  The alternative is to do:
    >
    >  wentOkay = True
    >  try:
    >    do something 1
    >    do something 2
    >
    >  except:
    >    print "Oops something didn't work"
    >    wentOkay = False
    >
    >  if wentOkay:
    >    do something 3
    >    do something 4
    >     ...
    >    do something 25
    >
    >
    > Which seems a bit verbose, but likely the better approach.  Is there some
    > other option I should be considering?


    Yes. try-except-*else*.

    try:
    do_something_1()
    do_something_2()
    except:
    print "Houston, we have a problem"
    else: #runs only if no exception was thrown
    do_something_3()
    do_something_4()
    et_cetera()

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    Chris Rebert, Feb 25, 2009
    #4
  5. RGK

    RGK Guest

    I'm glad I asked :)

    Thanks all who posted for your replies, the else-statement is a nice
    option.

    Python again comes through to deal with those pesky feelings that
    something could be better :)

    Ross.



    Chris Rebert wrote:

    > Yes. try-except-*else*.
    >
    > try:
    > do_something_1()
    > do_something_2()
    > except:
    > print "Houston, we have a problem"
    > else: #runs only if no exception was thrown
    > do_something_3()
    > do_something_4()
    > et_cetera()
    >
    > Cheers,
    > Chris
    >
    RGK, Feb 25, 2009
    #5
    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. pekka niiranen
    Replies:
    1
    Views:
    313
    Duncan Booth
    Oct 11, 2004
  2. John Salerno
    Replies:
    20
    Views:
    826
    John Salerno
    Aug 11, 2006
  3. Fabio Z Tessitore

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

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

    try -> except -> else -> except?

    David House, Jul 6, 2009, in forum: Python
    Replies:
    2
    Views:
    324
    Bruno Desthuilliers
    Jul 6, 2009
  5. MRAB
    Replies:
    4
    Views:
    299
Loading...

Share This Page