Exceptions as a Control Structure
Peter Hansen
peter at engcorp.com
Mon Aug 9 09:50:25 EDT 2004
Olivier Parisy wrote:
> Peter Hansen wrote:
>> 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).
>
> Do you have some example of this in the standard library ?
It's easy to check for yourself, you have the source. ;-)
I searched for "(Exception" in the lib/*.py files, since
this code is likely to occur when someone subclasses the
top-level exception class.
There's an example similar to what I was describing in
inspect.py, using an EndOfBlock exception. The Queue
module has Full and Empty exceptions that are a good
example. By far the most occurrences of custom
exceptions are to define specialized ones denoting
error conditions, however. I suspect the usage I
described is more common outside the standard library,
in part because code where it would be useful is more
likely to occur in higher level code.
-Peter
More information about the Python-list
mailing list