subclassing Exceptions

Sheila King sheila at spamcop.net
Sun Jul 22 14:36:07 EDT 2001


On Sun, 22 Jul 2001 10:55:10 -0700, Erik Max Francis <max at alcyone.com>
wrote in comp.lang.python in article <3B5B137E.3606B75D at alcyone.com>:

:Sheila King wrote:
:
:> Thanks, Brian, but I've seen the docs many times. I need more info
:> than that. Like example code, or source code.
:
:The library reference quoted states pretty clearly:
:
:	The root class for exceptions. All built-in exceptions are
:	derived from this class. All user-defined exceptions should
:	also be derived from this class, but this is not (yet) enforced. 
:	The str() function, when applied to an instance of this
:	class (or most derived classes) returns the string value of the
:	argument or arguments, or an empty string if no
:	arguments were given to the constructor. When used as a
:	sequence, this accesses the arguments given to the
:	constructor (handy for backward compatibility with old code). 
:	The arguments are also available on the instance's args
:	attribute, as a tuple.
:
:This gives you a pretty effective specification for what a class derived
:from exceptions.Exception should do; it should return something
:meaningful in its __str__ method, support __len__ and __getitem__ for
:indexing, and have an `args' attribute that contains the arguments it
:was instantiated with as a tuple.
:
:The Exception class itself does this.  If all you want is a different
:_type_ of exception that you can key off of, then deriving from the
:class and doing nothing with it (using `pass' as you did in your
:example) is perfectly satisfactory.  If you want to do more, then you
:can add extra methods as you need that use the value gained through
:calling `str' or the `args' attribute.
:
:What more did you need to know?

I'm trying to see the "big picture". I'm a "big picture" kind of gal. I
don't feel comfortable working with just bits and pieces...just enough
information (in the information-giver's opinion) for me to get by.

If I want to really understand subclassing Exceptions, and whether or
not I would ever want to override any of the standard exception classes
methods or such, then I need to understand how those exceptions work.

Like I said, I've been working (a bit) so far with subclassing
rfc822.py, and for me, I needed to read the source code of that module
and see how it worked. Then *bing* a light went off in my head, I
understood, and I could proceed.

Does this mean there is something wrong with me? Should I be satisfied
with the docs only?

Someone else gave me a good suggestion on the Tutor List, that if I was
really interested in how the Exception class works, I should grab the
source for Python 1.5.2, before the exception class code was written in
C. I might do that, but this discussion (and another on the Tutor List)
has helped immensely, and I may be fine from here.

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/





More information about the Python-list mailing list