[Python-Dev] Dangerous exceptions (was Re: Another test_compiler mystery)

Jeremy Hylton jhylton at gmail.com
Mon Sep 6 03:42:44 CEST 2004


On Sun, 5 Sep 2004 01:02:35 -0400, Tim Peters <tim.peters at gmail.com> wrote:
> I would like to see Python's exception hierarchy grow more
> sophisticated in this respect.  MemoryError, SystemExit, and
> KeyboardInterrupt are things that should not be caught by "except
> Exception:", neither by a bare "except:", nor by hasattr() or C-level
> dict lookup.  ZODB's ConflictError is another of that ilk.  I'd like
> to see "except Exception:" become synonymous with bare "except:", and
> move the "dangerous exceptions" to subclass off a new branch of the
> exception hierarchy.  It could be that something like your patch is
> the only practical way to make this work in the C implementation, so
> I'm keen on it.

The current exception hierarchy isn't too far from what you suggest. 
We just got the names wrong.  That is, there is a base class,
StandardException, that captures most exceptions other than
MemoryError, SystemError, and KeyboardInterrupt.  If we renamed that
Exception, then we'd be 90% of the way there.  You could also change
your code, right now, to say "except StandardError:" and avoid the
problem entirely.  Make sure ConflictError does not inherit from
StandardError, of course.  And make sure you're happy that ImportError
is not a StandardError either.

I'm not sure what I think of the change to "except:"  It's often the
case that someone who has written "except:" really means "except
Something:", but I expect that very often Something != StandardError
and issubclass(Something, StandardError).  In that case, the change
doesn't really help them.  The code is still wrong.

Jeremy


More information about the Python-Dev mailing list