## CatchDivByZero.pl use strict; use warnings; use Error qw(:try); use Arithmetic; use DivByZero; my $a = 10; my $b = 0; my $result; try { $result = Arithmetic::divide($a,$b); } catch DivByZero with { ## catch it here and not in the Class Arithmetic.pm $b = 1; ## so we can still do something else and not just die there $result = Arithmetic::divide($a,$b); } catch Error with { my $e = shift; print "Unhandled Exception $e\n"; }; printf "The result is %d\n", $result;
## Arithmetic.pm package Arithmetic; use strict; use warnings; use Error qw(:try); use DivByZero; sub divide { my ($a, $b) = @_; my $r; throw DivByZero if $b == 0; $r = $a/$b; return $r; } 1;
## DivByZero.pm package DivByZero; use Moose; extends 'Error'; 1;