Exceptions as a Control Structure

John J. Lee jjl at pobox.com
Tue Aug 10 22:50:54 CEST 2004

[Peter Hansen]
> Exceptions are also used by some as a mechanism for
> returning information from subroutines, though it's
> very likely the information will still be considered
> "exceptional" in some way (think of it as an out-of-band
> mechanism for returning special info).

[Olivier Parisy]
> Do you have some example of this in the standard library ?

Not sure if this is what Peter had in mind, but grep for 'ValueError'.

> While some people object on stylistic grounds (or even
> performance ones, in some cases) to such things, you
> will likely find that exceptions are thrown around
> by Python programmers more readily than you are used to.

[Olivier Parisy]
> That's what I wanted to know. OK.

Control-structure-y uses (esp. within a single function) are much
rarer than the ordinary use: a function signalling an exceptional
condition to its callers.  This ordinary use is certainly used more
extensively in idiomatic Python code than is the case in many other

StopIteration is used a lot, I guess, but it's rare that you're
actually concious of that fact.


More information about the Python-list mailing list