On 18-05-16 13:07, Richard Hainsworth wrote:
use v6;
my $d = 1;
my $e = 2;
my $f = 0;
#my $r;
my $r = 5;
CATCH {
# when X::AdHoc {
when Exception {
.Str.say;
# $r = 5;
.resume
} }
$r = try {
( $d + $e ) / $f;
};
# my $r = try EVAL ' ( 1 + 2 ) /0 ';
say "r is $r";
Hi Richard,
There are a few things to mention;
Write CATCH in a block. In that block you must test the things which
might go wrong. Like so;
try {
...
CATCH {
when .... {
....
}
default {
...
}
}
}
Below there is some other code about your example;
my $r;
{
try {
my $d = 1;
my $e = 2;
my $f = 0;
$r = ( $d + $e ) / $f;
say "r is $r";
CATCH {
.WHAT.say;
when X::Numeric::DivideByZero {
$r = 65;
.resume;
}
default {
$r = 55;
.resume;
}
}
}
say "r is $r";
}
This will not produce your desired answer but the following;
(DivideByZero)
(NoMatch)
(AdHoc)
This exception is not resumable
in block at t0-email.pl6 line 26
in block at t0-email.pl6 line 14
in block <unit> at t0-email.pl6 line 7
You see that '.WHAT.say' shows several types of exceptions. This is
caused by the resumes and in the end it gets one which is not resumable.
When you remove both resume statements you will get the output 'r is 65'
which is what you want. Also here you can see that the test against
X::Numeric::DivideByZero is taken.
Hope this helps,
Marcel