hackdaddy has asked for the wisdom of the Perl Monks concerning the following question:



I am working on implementing exception handling in a object-oriented Perl application. Currently, I am looking at using Error.pm. In the synopsis section of the documentation for Error.pm (listed below), there are some interesting lines of code.

try { do_some_stuff(); die "error!" if $condition; throw Error::Simple -text => "Oops!" if $other_condition; } catch Error::IO with { my $E = shift; print STDERR "File ", $E->{'-file'}, " had a problem\n"; } except { my $E = shift; my $general_handler=sub {send_message $E->{-description}}; return { UserException1 => $general_handler, UserException2 => $general_handler }; } otherwise { print STDERR "Well I don't know what to say\n"; } finally { close_the_garage_door_already(); # Should be reliable }; # Don't forget the trailing ; or you might be surprised


In the except clause, exceptions are handled by a general_handler anonymous sub which sends a message. Then a hash of the exceptions are returned. How would this work in the context of an application? What is the philosophy of this kind of exception handling? How would a class that receives exception/error messages influence the decision-making of higher level classes?

In the case of my application, I need to process all the errors that occur during a make process and then make decisions based on the errors in higher level classes.

I am looking for examples in Perl of an object-oriented application that uses Error.pm. I am also seeking guidance in the design of exception handling and error handling in Perl object-oriented applications.

Replies are listed 'Best First'.
Re: Exception Handling using Error.pm
by perrin (Chancellor) on May 09, 2002 at 18:17 UTC
    The "except" clause is just a quick way of catching a whole set of exceptions without writing a separate "catch" clause for each one. I don't think you would have any need for that in most cases.

    If you want some examples of using exceptions, look at my article or this presentation by Matts.

Re: Exception Handling using Error.pm
by P0w3rK!d (Pilgrim) on May 10, 2002 at 17:33 UTC
    In this context the except clause is allowing you to use different error handling in one statement. depending on the error, you can call whatever handler you want.
    As for influencing the decision-making of higher-level classes, I do not have an answer for you. :)
    -P0w3rK!d
Re: Exception Handling using Error.pm
by hackdaddy (Hermit) on May 10, 2002 at 23:58 UTC
    Thank you for your replies.

    So far the best example of exception handling I have been able to find is from the bioperl project.

    It would be very insightful if someone could create a skeleton of classes using Error.pm that illustrates its use in exception handling. Another item of interest is the way that Error.pm implements the syntactic sugar for the  try {} catch {} except {} otherwise {} finally {}; statement.

    Also of interest:
    The Problem with Perl's Exceptions.
    Exceptions,ExceptionsClass, and a quick hack.