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?
MyExceptions::SomeThingy
MyExceptions::SomeThingy::SomeSpecificError
MyExceptions::SomeThingy::AnotherProblem
MyExceptions::SomeThingy::DontDoThat
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:
MyException::SomeThingy' => {
isa => 'MyException',
description => 'Bad things happen here.',
fields => [ 'descriptor', 'value' ],
};
And then I throw them:
MyException::SomeThingy->throw(
'error' => 'You did something I didnt like.',
'descriptor' => 'DontDoThat',
'value' => $bad_variable,
);
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.
UPDATE:
This page covers what I was asking, too, in the paragraph titled "Write Sensible Throws Clauses".
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.