Try/except vs. if/else

Tim Rowe tim at remove_if_not_spam.digitig.co.uk
Mon Sep 29 05:33:27 EDT 2003


On 27 Sep 2003 01:25:10 -0700, hungjunglu at yahoo.com (Hung Jung Lu)
wrote:

>Tim Rowe <tim at remove_if_not_spam.digitig.co.uk> wrote in message news:<u941nvg94ae04ajndvbjs2hlqc9l5lemqh at 4ax.com>...
>> On Tue, 23 Sep 2003 11:10:49 -0400, Shu-Hsien Sheu <sheu at bu.edu>
>> wrote:
>> 
>> >In my understanding, using try/except rather than if/else is more 
>> >pythonic. 
>> 
>> Rule of thumb: when the block of code is still doing what it's
>> supposed to do, use if/else. If it's failing to do what it's supposed
>> to do, use try/except. "except" should be an /exception/!
>.....
>> So try/except /only/ when something has gone wrong and you need to go
>> into some sort of recovery or termination, /not/ for routine tests.
>
>You have a valid point of view, which nonetheless is not shared by
>everyone. This is a recurring subject in the newsgroup.

<examples snipped>

That's the nice thing about rules of thumb: they're not binding :-)

Internal constructs don't bother me -- ultimately it all comes down to
machine code which will be a mass of goto's.  That doesn't mean that
my code should be (could be!) a mass of goto's.

And the last time I needed a goto for anything I was programming in
BASIC; I don't need to use exceptions to emulate it in a language with
decent flow control constructs (Python qualifies, of course!).




More information about the Python-list mailing list