Perfectly valid concern! ;-). I actually very much enjoy our
discussion.
Let me add a few points then to leverage your understanding
of the advantages involved.
Yet another great advantage of OO-ish exception handling is
be that different types of errors could be dealt with
at different 'layers' of your code. For example,
I might have a system exception in my code handled by Error::Sys.
Such an error could be thrown but the inner-most subroutine
and picked up and processed only a few levels up the call tree.
(yet not at the very top of it... say at the main:: package).
There may be a number of other exception types/categories
that are only handled by certain layers of your code.
In such case, OO-ish way is slightly batter compared to eval/die.
Making same with eval/die would (if possible) be somewhat
cumbersome (at least the way I see it :). In order to weed
certain die messages at various layers of my code, I might
have to institute a mechanism to make sure that every die
message has a particular keyword in it. For example,
for a system error I might have "SYSTEM:
error text".
Then, later in the code (or up the call tree), I'll check
for a match with that key word. And if matched, I'll process
it accordingly instead of leaving it to be handled
by upper-layer code (sorry if my terms sound amusing :-> ..).
With OO, you may simply do something like this:
# called from sub foobar() which in turn
# is called from foo() which in turn...
sub do_something {
...
try {
stuff();
} catch Error::Sys with {
# handly any System error right here
};
# any other error will go up the call tree..
}
With eval/die similar code would have to be implemented
as follows:
# called from sub foobar() which in turn
# is called from foo() which in turn...
sub do_something {
...
eval {
stuff();
};
my $err = $@;
if ($err =~ m/SYSTEM:/) {
# handle any System error right here
} elsif ($err) {
# any other error will go up the call tree..
die $err; # rethrow
}
}
I'm sure there's tons of other examples that could be brought
up. I hope that the ones I mentioned are reasonable ;-)
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.