<?xml version="1.0" encoding="windows-1252"?>
<node id="518228" title="Exception Classes Advice on Subclasses" created="2005-12-20 22:44:26" updated="2005-12-20 17:44:26">
<type id="115">
perlquestion</type>
<author id="511276">
je44ery</author>
<data>
<field name="doctext">
I'm using Exception::Class to define my exceptions classes, but I just stumbled into a design question.  Should create lots of exception subclasses like this?

&lt;code&gt;
MyExceptions::SomeThingy
MyExceptions::SomeThingy::SomeSpecificError
MyExceptions::SomeThingy::AnotherProblem
MyExceptions::SomeThingy::DontDoThat
&lt;/code&gt;

But if the subclasses SomeSpecificError, AnotherProblem, DontDoThat aren't different, then why have them? I was thinking of replacing those subclasses with fields under SomeThingy.  Like this:

&lt;code&gt;
MyException::SomeThingy' =&gt; {
        isa         =&gt; 'MyException',
        description =&gt; 'Bad things happen here.',
        fields      =&gt; [ 'descriptor', 'value' ],
};
&lt;/code&gt;

And then I throw them:

&lt;code&gt;
MyException::SomeThingy-&gt;throw(
            'error'      =&gt; 'You did something I didnt like.',
            'descriptor' =&gt; 'DontDoThat',
            'value'   =&gt; $bad_variable,
);
&lt;/code&gt;

Hmmm.  So I guess my question is, if all those subclasses do the same thing, am I getting anything out of creating them all?  Other than the name of the exception itself tells me something about the error?  If that is all I am gaining, then doesn't using fields make more sense?

I'm new to OO, and new to using Perl as anything other than a glorified awk.  Thanks.
&lt;p&gt;
&lt;b&gt;UPDATE:&lt;/b&gt;
[href://http://java.sun.com/developer/technicalArticles/Programming/exceptions/|This page] covers what I was asking, too, in the paragraph titled  "Write Sensible Throws Clauses".</field>
</data>
</node>
