Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > coding style - try, except

Reply
Thread Tools

coding style - try, except

 
 
RGK
Guest
Posts: n/a
 
      02-25-2009

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.

 
Reply With Quote
 
 
 
 
Steve Holden
Guest
Posts: n/a
 
      02-25-2009
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/

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      02-25-2009
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
 
Reply With Quote
 
Chris Rebert
Guest
Posts: n/a
 
      02-25-2009
On Wed, Feb 25, 2009 at 9:36 AM, RGK <(E-Mail Removed)> 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
 
Reply With Quote
 
RGK
Guest
Posts: n/a
 
      02-25-2009

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
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
try -> except -> else -> except? David House Python 2 07-06-2009 05:48 PM
who is simpler? try/except/else or try/except Fabio Z Tessitore Python 5 08-13-2007 12:52 AM
converting a nested try/except statement into try/except/else John Salerno Python 20 08-11-2006 02:48 PM
general coding issues - coding style... calmar Python 11 02-21-2006 10:36 AM
Help needed: try/except -idiom vs. C-style coding pekka niiranen Python 1 10-11-2004 02:14 PM



Advertisments