Exceptions are not just for errors (was: My first Python program)
emmanuel.surleau at gmail.com
Wed Oct 13 23:09:49 CEST 2010
> Seebs <usenet-nospam at seebs.net> writes:
> > On 2010-10-13, Chris Rebert <clp2 at rebertia.com> wrote:
> > > For future reference, the significant majority of things in Python
> > > raise exceptions upon encountering errors rather than returning
> > > error values of some sort.
> > Yes. I'm getting used to that -- it's a bit of a shift, because I'm
> > used to exceptions being *exceptional* -- as in, not a failure mode
> > you would expect to see happening.
> From that expectation, it's an even more fundamental shift. Python
> encourages (and tends toward) raising exceptions in any exceptional
> circumstance — that is, in any circumstance that isn't the usual
> behaviour for the function.
> So, for example, an exception (StopIteration) is how iterables signal
> that they've finished iterating; but that's not an error, only an
> exception. Likewise for the ‘str.index’ example given earlier.
I'd say more succintly that Python uses (at times) exception for flow control.
Whether that's elegant or not is debatable.
More information about the Python-list