I used Java before I started using Perl heavily. Whatever else you might say about the language, I must say that the exception handling is great. In Perl, Error.pm replicates much of Java's exception handling (and adds a few things of its own, but I've never used those parts so I can't comment on them).

Back when I was doing Java, I realized that much of the time the exception classes are internally identical. You only need to seperate them from other exceptions for the benifit of the application.

For instance, say you are using DBI, and you want to throw exceptions if it fails to connect, if a prepare() fails, or if an execute() fails. In one module I'm working on right now, I write three seperate exceptions for this, in the following inheirtance tree:

This is really easy to implement in Perl. At the bottom of my module, I declare all my exceptions like this:

package My::DBException; use base 'Error::Simple'; package My::DBPrepareException; use base 'My::DBException'; package My::DBExecuteException; use base 'My::DBException';

All the hard work was already done in Error::Simple. The application knows what went wrong because it has (should have) seperate catch . . . with { } blocks for each exception type.

Note that the equivilent in Java requries having a seperate file for your exception (assuming it's used outside your specific class), inheirting from java.lang.Exception, and declaring your own constructor. This will take more than two-lines per exception. (Apoligies if any of the previous wasn't quite right--I've been away from Java for a while).

----
I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
-- Schemer

Note: All code is untested, unless otherwise stated


In reply to Re: Intelligent Exception Handling in Perl by hardburn
in thread Intelligent Exception Handling in Perl by skyknight

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.