coding style - try, except

R

RGK

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.
 
S

Steve Holden

RGK said:
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
 
P

Peter Otten

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 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
 
C

Chris Rebert

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
 
R

RGK

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top