[Python-Dev] Exceptions

Vladimir Marangozov Vladimir.Marangozov@inrialpes.fr
Fri, 24 Dec 1999 13:07:05 +0100 (CET)


Guido van Rossum wrote:
> 
> Vladimir.Marangozov@inrialpes.fr:
> 
> > Yes. Besides, I still think that string-based exceptions are just
> > convenient for quick & dirty, throw-away test scripts.
> 
> They have a hard-to-understand quirk though: the id() of the string is
> used to check rather than its value, so that except "foo" doesn't
> necessarily catch raise "foo"; but due to various optimization, this
> usually works, and people get bent out of shape when it doesn't.

Which brings 2 important questions:

1. In the long run, which one is better -- compare and check exceptions by
   reference (by name) or by value?

   (currently, this is done by reference on predefined object types:
    strings, classes or instances)

   I'd say, exceptions have to be compared (catched) by value, i.e. use
   "e1 == e2" instead of "e1 is e2".

2. Should we limit the exception "types"?

   I'd say, no. My Pythonic view of things says that we raise "objects",
   be they classes, instances, strings or, why not, ints.

   However, if one wants to put some order in the "unordered set" of exceptions
   s/he uses, then classes is the way to do it, because classes were given some
   nice properties, like inheritance, that allow to group and to organize logically
   the objects we throw and catch as exceptions (+ other bonus properties coming
   from classes).

   Note that conceptually, when we say "strings and ints", we have in mind
   "string instances and int instances", whose "classes" are written in C.
   When there will be String and Int classes of some sort as first class objects,
   then we'll fall back to the terminology: Exceptions can be classes or instances.

If point 1 and (optionally) point 2 is implemented, the hard-to-understand quirk
wouldn't be an issue and string-based exceptions would have a legal reason to stay
and live.

> Since you have to give your exception a name, how hard is it to say
> 
> class MyError(Exception): pass
> 
> rathern than
> 
> MyError = "MyError"
> 
> ?

You know what I think about "names"...  I may have defined my exception conventions
and be interested in catching an exception named 404, implying that "a 404 bobo"
occured deeply in my code ("deeply in my code" meaning for example: database 4,
service 0, customer group 4, or just a standard HTTP "Code 404 - Not Found".)

Pushing this to the extreme to catapult your thoughts into the next millenium. :)
and to emphasize the importance of discussing and anwsering objectively the above
questions 1) and 2).

-- 
       Vladimir MARANGOZOV          | Vladimir.Marangozov@inrialpes.fr
http://sirac.inrialpes.fr/~marangoz | tel:(+33-4)76615277 fax:76615252