[Python-Dev] PEP, take 2: Exception Reorganization for Python 3.0

Nick Coghlan ncoghlan at gmail.com
Wed Aug 3 15:10:33 CEST 2005


Phillip J. Eby wrote:
> +1.  The main things that need fixing, IMO, are the need for critical and 
> control flow exceptions to be distinguished from "normal" errors.  The rest 
> is mostly too abstract for me to care about in 2.x.

I guess, before we figure out "where would we like to go?", we really need to 
know "what's wrong with where we are right now?"

Like you, the only real problem I have with the current hierarchy is that 
"except Exception:" is just as bad as a bare except in terms of catching 
exceptions it shouldn't (like SystemExit). I find everything else about the 
hierarchy is pretty workable (mainly because it *is* fairly flat - if I want 
to catch a couple of different exception types, which is fairly rare, I can 
just list them).

I like James's suggestion that instead of trying to switch people to using 
something other than "except Exception:", we just aim to adjust the hierarchy 
so that "except Exception" becomes the right thing to do. Changing the 
inheritance structure a bit is far easier than trying to shift several years 
of accumulated user experience. . .

Anyway, with the hierarchy below, "except Exception:" still overreaches, but 
can be corrected by preceding it with "except (ControlFlow, CriticalError): 
raise".

"except Exception:" stops overreaching once the links from Exception to 
StopIteration and SystemExit, and the links from StandardError to 
KeyboardInterrupt, SystemError and MemoryError are removed (probably difficult 
to do before Py3k but not impossible).

This hierarchy also means that inheriting application and library errors from 
Exception can continue to be recommended practice. Adapting the language to 
fit the users rather than the other way around seems to be a pretty good call 
on this point. . .

The only changes from the Python 2.4 hierarchy are:
    New exceptions:
      - Raisable (new base)
      - ControlFlow (inherits from Raisable)
      - CriticalError (inherits from Raisable)
      - GeneratorExit (inherits from ControlFlow)
    Added inheritance:
      - Exception from Raisable
      - StopIteration, SystemExit, KeyboardInterrupt from ControlFlow
      - SystemError, MemoryError from CriticalError

Python 2.4 Compatible Improved Exception Hierarchy v 0.2 [1]
============================================================

   Raisable (new)
   +-- ControlFlow (new)
       +-- GeneratorExit (new)
       +-- StopIteration (inheritance new)
       +-- SystemExit (inheritance new)
       +-- KeyboardInterrupt (inheritance new)
   +-- CriticalError (new)
       +-- MemoryError (inheritance new)
       +-- SystemError (inheritance new)
   +-- Exception (inheritance new)
       +-- StopIteration
       +-- SystemExit
       +-- StandardError
           +-- KeyboardInterrupt
           +-- MemoryError
           +-- SystemError
           +-- AssertionError
           +-- AttributeError
           +-- EOFError
           +-- ImportError
           +-- TypeError
           +-- ReferenceError
           +-- ArithmeticError
               +-- FloatingPointError
               +-- DivideByZeroError
               +-- OverflowError
           +-- EnvironmentError
               +-- OSError
                   +-- WindowsError
               +-- IOError
           +-- LookupError
               +-- IndexError
               +-- KeyError
           +-- NameError
               +-- UnboundLocalError
           +-- RuntimeError
               +-- NotImplementedError
           +-- SyntaxError
               +-- IndentationError
                   +-- TabError
           +-- ValueError
               +-- UnicodeError
                   +-- UnicodeDecodeError
                   +-- UnicodeEncodeError
                   +-- UnicodeTranslateError
       +-- Warning
           +-- DeprecationWarning
           +-- FutureWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning

Cheers,
Nick.

[1] I've started putting version numbers on these suggestions, since someone 
referred to "Nick's exception hierarchy" in one of the threads, and I had no 
idea which of my suggestions they meant. I think I'm up to three or four 
different variants by now. . .

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com


More information about the Python-Dev mailing list