Greetings!
I am using Error.pm at the moment on a relatively big project without much of a hussle. From this experience, using Error.pm is probably better than doing your own exception code for the following reasons:
- nice (and predictable) layout of exceptions hyerarchy (by using your own exception base class you get the whole thing, and it croaks when you try to throw an Exception you don't have)
- makes the checking easier (if you miswrite your class name on your examplo above, you'll never treat that exception right, but you cannot catch with (or throw) an unknown exception
- makes the code easily maintainable and easier on the reading than barebones eval/if($@), and this is Good(tm)
My take on exception handling was this (just in case it may be useful to you), which is surely improveable, but I'm not going to touch it now ;-)
- use Error.pm as the base class of my hyerarchy of exceptions
- use 'throw' or 'die with' to generate exceptions
- use Log::Log4perl (but surely any other logging way will work) to leave the exceptions message (so that the information about the failure is logged down, and it's the type (or class) of the Exception what's propagated to the upper levels (so the log has detailed info about the failure, usually with different info at different logging 'depths' (i.e. simpler for ERROR but more verbose for DEBUG), and simple reasons for the exceptions messages themselves...)
- following Erlang's failure system, I just catch (by using eval or the module's :try keywords) the exceptions I know how to treat and let everything else to float up to the toplevel, where appropriate actions (usually dying) are taken
briefly: extensive logging and a nice exception hyerarchy with the alternate constructors from Error.pm which make everything easier to read.
Using try/catch with/catch/otherwise has worked nicely in some parts of the project and horribly in others (hosted Perl code which mangles with the C server that manages the Perl code, making dying really bad (lost place of dying, lost of cause and the stacktrace, etc...), so YMMV...
Good luck!
--
our $Perl6 is Fantastic;
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.