On 05/18/2016 01:39 PM, mt1957 wrote:
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 {
      ...
      }
    }
}

if you have an explicit CATCH block, you don't need a try { ... } on the outside.

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;
        }

If you want to only catch exceptions of type X::Numeric::DivideByZero, don't supply a default block.

Cheers,
Moritz

Reply via email to