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
|
use strict;
use warnings;
use Test::More;
use TryCatch;
sub nested_1 {
try {
try {
return "from nested_1";
}
catch ($e) {
}
}
}
sub nested_2 {
try {
nested_1();
return "from nested_2";
}
}
is( nested_1(), "from nested_1", "nested try");
is( main->nested_1(), "from nested_1", "nested try (as method)");
is( nested_2(), "from nested_2", "call nested try");
is( main->nested_2(), "from nested_2", "call nested try (as method)");
# same thing, but now we return from within the catch
sub nested_catch {
try {
try {
die "Some str\n";
}
catch ( $e ) {
return "return from nested catch";
}
}
return "didn't catch";
}
is( nested_catch(), "return from nested catch", "nested catch" );
my $val;
try {
try { die "Foo" }
catch ($e) { die "$e" }
}
catch ($e) {
$val = "$e";
}
like($val, qr/^Foo at t[\/\\]nested.t line /,
"Nested try-catch in same function behaves");
sub nested_rethrow {
try {
try {
die "Some str\n";
}
catch (Str $err where { length $_ < 5 }) {
return "caught in inner TC";
}
}
catch {
return "caught in outer TC";
}
return "didn't catch";
}
is( nested_rethrow(), "caught in outer TC", "nested rethrow" );
done_testing;
|