I am trying to incorporate cleaner exception handling in my code. Here 
is a small illustration (meaningless) of what most of my code looks 
like:

package main;
use warnings;
use strict;

my $average = Computer::get_average();
unless ($average) {
    print "$Computer::ERROR\n";
}

1;


package Computer;
use strict;
use warnings;

our $ERROR;
sub get_average {

    my $values = Databank::get_values();

    unless ($values) {
        $ERROR = "Can not compute average:\n$Databank::ERROR";
        return undef;
    }

    #do some useful stuff we never get to do
}

1;


package Databank;
use strict;
use warnings;

our $ERROR;

sub get_values {
    #fail right away for the sake of the test

    $ERROR = "Sorry, bank is empty";
    return undef;
}

1;


I then tried to change it in the following way, but I get an error:
Can't locate object method "catch" via package "DBException" (perhaps 
you forgot to load "DBException"?) at ./testerror line 16.


package main;
use warnings;
use strict;
use Error qw(:try);

try {
my $average = Computer::get_average();
}

catch DBException with {
    my $ex = shift;
    print ("An error occured: " . $ex->getMessage() );
    exit 1;
};

1;



package Computer;
use strict;
use warnings;

sub get_average {

    my $values = Databank::get_values();

    #do some useful stuff we never get to do
}

1;


package Databank;
use strict;
use warnings;
use Error qw(:try);

sub get_values {

    #fail right away
    throw DBException ("Sorry, bank is empty");
}

1;


I guess I am missing something fundamental here. Could anyone be kind 
enough to help me out?

Peter

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to