Try/except vs. if/else
Hung Jung Lu
hungjunglu at yahoo.com
Sat Sep 27 10:25:10 CEST 2003
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>
> >In my understanding, using try/except rather than if/else is more
> 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.
Python exceptions have been used for other purposes, as can be seen
from Python FAQ (e.g. "4.22 Why is there no goto?" in
The "for" loop in Python is also implemented internally with
exceptions. E.g.: http://groups.google.com/groups?selm=mailman.1010884758.23378.python-list%40python.org&oe=UTF-8&output=gplain,
where it mentioned:
"... In some other languages, 'non failure' mode exceptions may be
unusual, but it's the normal idiom in Python."
More information about the Python-list