subclassing Exceptions

Sheila King sheila at spamcop.net
Sun Jul 22 02:20:14 EDT 2001


On Sun, 22 Jul 2001 00:51:31 -0500, "Volucris" <volucris at hotmail.com>
wrote in comp.lang.python in article
<3b5a69a3$0$320$6e49188b at news.goldengate.net>:

:If all you want is your own exception (nothing fancy), all you need is a
:class:
:
:class SpamException:
:    pass

OK, interesting. I didn't realize you could make an exception without
subclassing it. So, I tried this script:

-----------begin except.py---------------------

class SpamException:
    pass

while (1):
    inputstring = raw_input("Enter any string, except 'spam': ")
    if inputstring == "spam":
        raise SpamException

-----------end except.py-----------------------

Here is a sample output:

>>> 
Enter any string, except 'spam': pig
Enter any string, except 'spam': spam
Traceback (innermost last):
  File "E:/programs/LearningPython/Exceptions/except.py", line 7, in ?
    raise SpamException
SpamException: <__main__.SpamException instance at 0104C94C>


:you can raise and catch that just like any exception, but it'll be kind of
:ugly when it gets printed.

Well, yes. It doesn't look like normal exceptions, does it?

:class SpamException:
:    def __str__(self):
:        return "cooking spam is not required"
:
:that will give a clue as to what the exception means. 

OK, so I modified my above file, to include the __str__ as you show.
Here is a sample run:

>>> 
Enter any string, except 'spam': horse
Enter any string, except 'spam': meat
Enter any string, except 'spam': spam
Traceback (innermost last):
  File "E:/programs/LearningPython/Exceptions/except.py", line 8, in ?
    raise SpamException
SpamException: cooking spam is not required

So, yes, it does look a bit nicer.

:That said, I don't
:recall ever seeing an exception not subclassed from Exception, so it's
:probably a good idea.
:
:class SpamException(Exception):
:    def __str__(self):
:        return "cooking spam is not required"
:
:I don't know why every one is subclassed, but the HPIC seem to know what
:they're doing so far. I have no reason to question them. Yet.

Interesting point. When I make this modification to the SpamException
class, deriving it from the Exception class, I seem to get the exact
same behavior.

However, I noticed earlier this evening, from browsing the exceptions.c
source (as noted elsewhere in this thread), that the Exceptions seem to
have 3 methods and 1 datamember, namely:

methods:
__init__
__getitem__
__str__

and the datamember
__doc__

So, I would imagine that if you subclass it, and do not specifically
define these methods/data listed above, that you at least inherit them
from the base class. Whereas, if you create an "exception" that does not
derive from a base Exception class, it would not have these methods.


Hmm...I just tried a little experiment in the interactive shell to see
if I could confirm/deny my above theory, and it gave me surprising
results that I do not understand, and which seem to indicate no
difference between deriving it from an Exception class or not.

Well, I am interested in further enlightenment on this topic...?

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





More information about the Python-list mailing list