Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 78 lines (61 sloc) 1.291 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
#! nqp

# Tests for try and catch

plan(8);

sub oops($msg = "oops!") { # throw an exception
    my $ex := Q:PIR { %r = new ['Exception'] };
    $ex<message> := $msg;
    pir::throw($ex);
}

my $ok := 1;
try {
    oops();
    $ok := 0;
}

ok($ok, "exceptions exit a try block");

sub foo() {
    try {
        return 1;
    }
    return 0;
}

ok(foo(), "control exceptions are not caught by a try block");

ok(try oops(), "statement prefix form of try works");

{
    CATCH { ok(1, "CATCH blocks are invoked when an exception occurs"); }
    oops();
}


$ok := 1;
sub bar() {
    CATCH { $ok := 0; }
    return;
}
bar();
ok($ok, "CATCH blocks ignore control exceptions");

$ok := 1;
{
    {
        {
            oops();
            CATCH { $ok := $ok * 2; pir::rethrow($!); }
        }
        CATCH { $ok := $ok * 2; pir::rethrow($!); }
    }
    CATCH { $ok := $ok * 2; pir::rethrow($!); }
    CATCH { ok($ok == 8, "rethrow and multiple exception handlers work") }
}

$ok := 1;

{
    for 1, 2, 3, 4 {
        $ok := $ok * 2;
        oops();
    }
    CATCH { my &c := $!<resume>; &c(); }
}

ok($ok == 16, "resuming from resumable exceptions works");

$ok := 0;
{
    CATCH { $ok := -1; }
    CONTROL { $ok := 1; }
    return 5;
}
ok($ok == 1, "CONTROL blocks catch control exceptions");
Something went wrong with that request. Please try again.