[Python-Dev] Exception hierarchy [was Re: Another test_compiler mystery]

James Y Knight foom at fuhm.net
Tue Aug 17 04:52:35 CEST 2004


On Aug 16, 2004, at 9:32 PM, Guido van Rossum wrote:

> Hm, Java needs the distinction because some exceptions must be
> declared and others mustn't.  But Python doesn't have that
> distinction.  I'm not sure that you can always treat the same set of
> exceptions as fatal.  E.g. in many situations, AttributeError,
> TypeError and NameError are all indicative of programming bugs
> (typically typos), but in other contexts these are recoverable.  So
> rather than giving an arbitrary definition of fatality, let's refrain
> from defining the concept.

Well, actually, java has three categories of exceptions:
1) Error - not necessary to declare as thrown, only *serious* errors. 
Contains:
     a) things that can "never happen" with a program compiled by the 
java compiler (no such field, unknown class file format, etc),
     b) machine errors (out of memory, stack overflow, etc)
     c) assert() failure
     d) ThreadDeath (similar to a KeyboardInterrupt/SystemExit)
2) Exception - normal run of the mill exception, needs to be declared 
as thrown.
3) RuntimeException - subclass of Exception, does not need to be 
declared as thrown. (e.g. IndexOutOfBoundsException, 
NoSuchFieldException, ClassCastException)

The distinction you refer to above is really the difference between 
Exception and RuntimeException. Translated to java, AttributeError, 
TypeError and NameError would be RuntimeExceptions. So, I agree with 
you - I don't believe python needs that distinction. I do believe 
python needs the distinction between Exception and Error.

> Calling SystemExit and KeyboardInterrupt fatal strikes me as
> particularly odd, as I routinely catch these.

I'll agree: I don't think the name "FatalError" is particularly great. 
However, I hope it gets the idea across better than 
"XXXErrorXXXRenameMeXXX" which was my other choice of name. ;) I do 
think the categorization is correct. While you may sometimes catch 
KeyboardInterrupt/SystemExit, most of the time you really do not want 
to, even if you are catching "everything". If you do want to catch 
KeyboardInterrupt, you are also likely to be catching it explicitly by 
its name, anyhow.

James



More information about the Python-Dev mailing list