The problems are not in the declaration, but in sub ThrowMyException. Here is a working version, with changes commented:
#! perl
use strict;
use warnings;
use Exception::Class
(
'MyException',
Commands =>
{
isa => 'MyException',
},
Timeout =>
{
isa => 'Commands',
description => 'This exception results from running the metric
+s commands',
},
DBError =>
{
isa => 'MyException',
description => 'DB returned error',
},
);
ThrowMyException();
print "No error caught\n";
sub ThrowMyException # NO prototype
{
eval {
Timeout->throw # NOT MyException::Commands::Timeo
+ut->
(
error => "This error is due to a timeout"
);
};
my $err;
if ($err = Exception::Class->caught('Timeout'))
{
die $err->description . # NOT $err->{"description"}
': ' .
$err->error; # NOT $err->{"error"}
}
elsif ($err = Exception::Class->caught('MetricsException'))
{
die $err->error;
}
else
{
$err = Exception::Class->caught();
ref $err ? $err->rethrow : die $err;
}
}
Output:
12:30 >perl 517_SoPW.pl
This exception results from running the metrics commands: This error i
+s due to a timeout at 517_SoPW.pl line 56.
12:33 >
Hope that helps,