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

Tim Peters tim.peters at gmail.com
Wed Sep 8 05:14:49 CEST 2004


After a bit more thought (and it's hard to measure how little), I'd
like to see "bare except" deprecated.  That doesn't mean no way to
catch all exceptions, it means being explicit about intent.  Only a
few of the bare excepts I've seen in my Python life did what was
actually intended, and there's something off in the design when the
easiest thing to say usually does a wrong thing.

I think Java has a saner model in this particular respect:

Throwable
    Exception
    Error

Java's distinction between "checked" and "unchecked" exceptions is a
distinct layer of complication on top of that.  All exceptions derive
from Throwable.  A "catch" clause requires specifying a class (there's
no "bare except").  "An Error is a subclass of Throwable that
indicates serious problems that a reasonable application should not
try to catch".  That includes AssertionError and VirtualMachineError. 
Those are exceptions that should never occur.  It also includes
ThreadDeath, which is expected to occur, but

    The class ThreadDeath is specifically a subclass of Error rather
than Exception,
    even though it is a "normal occurrence", because many applications catch all
    occurrences of Exception and then discard the exception.

and it's necessary for ThreadDeath to reach the top level else the
thread never really dies.

In that respect, it's interesting that SystemExit and
KeyboardInterrupt are *intended* to "reach the top level" too, but
can't be relied on to do so because of ubiquitous bare excepts and
even pseudo-careful "except Exception:"s now.  If people changed those
to "except StandardError:", SystemExit would make it to the top but
KeyboardInterrupt still wouldn't.

Raisable
    Exception
    Stubborn
        ControlFlow
            KeyboardInterrupt
            StopIteration
            SystemExit
        MemoryError

introduces a class of stubborn exceptions, those that wouldn't be
caught by "except Exception:", and with the intent that there's no way
you should get the effect of "except Raisable" without explictly
saying just that (once bare except's deprecation is complete).

Oh well.  We should elect a benevolent dictator for Python!


More information about the Python-Dev mailing list