[Python-Dev] PEP 348: Exception Reorganization for Python 3.0

Brett Cannon bcannon at gmail.com
Fri Aug 5 21:02:46 CEST 2005


On 8/5/05, Raymond Hettinger <raymond.hettinger at verizon.net> wrote:
> The PEP moves StopIteration out from under Exception so that it cannot
> be caught by a bare except or an explicit "except Exception".
> 
> IMO, this is a mistake.  In either form, a programmer is stating that
> they want to catch and handle just about anything.  There is a
> reasonable argument that SystemExit special and should float to the top,
> but that is not the case with StopIteration.
> 
> When a user creates their own exception for exiting multiple levels of
> loops or frames, should they inherit from ControlFlowException on the
> theory that it no different in intent from StopIteration or should they
> inherit from UserError on the theory that it is a custom exception?

I say ControlFlowException.  UserError is meant for quick-and-dirty
exception usage and not as a base for user error exceptions.  If the
name is confusing it can be changed to SimpleError.

>  Do
> you really want routine control-flow exceptions to bypass "except
> Exception".

Yes.

>  I suspect that will lead to coding errors that are very
> difficult to spot (it sure looks like it should catch a StopIteration).
> 

I honestly don't think it will.  People who are going to care about
catching StopIteration are writing custom iterators, not something a
newbie will porobably be doing and thus should know to be specific
about what exceptions they are catching when they have a specific
thing in mind.

> Be careful with these proposals.  While well intentioned, they have
> ramifications that aren't instantly apparent.  Each one needs some deep
> thought, user discussion, usability testing, and a darned good reason
> for changing what we already have in the field.
> 

Right, which is why this is all in a PEP, so the discussion can happen
and the kinks can be worked out.  As for the testing, that can happen
with __future__ statements, people trying out a patch, or maybe even
some testing branch of Python for possible 3000 features.

-Brett


More information about the Python-Dev mailing list