subclassing Exceptions
Erik Max Francis
max at alcyone.com
Sun Jul 22 13:55:10 EDT 2001
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?
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/ \ But since when can wounded eyes see / If we weren't who we were
\__/ Joi
Rules for Buh / http://www.alcyone.com/max/projects/cards/buh.html
The official rules to the betting card game, Buh.
More information about the Python-list
mailing list