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
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |