subclassing Exceptions

Sheila King sheila at spamcop.net
Sun Jul 22 14:23:21 EDT 2001


On Sun, 22 Jul 2001 07:46:25 +0500, Tom Bryan <tbryan at python.net> wrote
in comp.lang.python in article <9jee61$2ac$1 at slb6.atl.mindspring.net>:

:Sheila King wrote:
:
:> OK, interesting. I didn't realize you could make an exception without
:> subclassing it. So, I tried this script:
:
:Neither did I, but you probably don't want to do that.

Right.

:One reason to subclass from a common Exception base class is so 
:that exceptions can be caught based on inheritence.  Also, since 
:there's a common exception base class, all exceptions have common 
:behavior that you'd have to recode if you didn't subclass.  

You know, late last night/early this morning, that just kind of slipped
past me. But it makes perfect sense (of course).

:> Well, I am interested in further enlightenment on this topic...?
:
:I believe that the intent is for all exception classes to subclass from 
:Exception or one of its sublcasses.  I think that a common idiom is for 
:a module to declare its own base exception class.  Then, it can declare 
:many subclasses of *that* Exception subclass to use internally.  People 
:using the module can simply catch the base FooException unless they 
:have a good reason to get more specific.  Basically, they called code 
:in your module, and it blew up.  Often, that's as much as they can say, 
:and the error message attached to the exception *should be* enlightening 
:enough to troubleshoot the problem.  

Thanks. This is exactly the type of stuff that I am currently working
with and need to know about.

:For example, 

[excellent code for stack class with exceptions, snipped]

:Of course, this advice might be slightly off.  Python's exception stuff looks 
:a lot like the Java exception stuff, and I've done a lot more Java.  

It looked awfully good to me. I'm subclassing some of the mail modules,
like rfc822 right now, and also another project working with RGB vs. Hex
color values and the correct way of writing my own Exceptions for these
modules has been preventing me from getting very far into it, since I
keep pausing and wondering how to handle it, and my overview of the
entire module is affected by how I want to handle this.

Thanks,

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




More information about the Python-list mailing list