Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Succinct way to change thrown Exception to Failure?

Is there a more succinct way to lift a thrown Exception to a Failure than the following?

try {
    die 'always';
    CATCH { default { fail $_ } }
}
like image 550
J Hall Avatar asked Apr 18 '18 02:04

J Hall


2 Answers

Here's a more succinct version of moritz' code.

(try something()) orelse fail $!;
like image 89
timotimo Avatar answered Sep 18 '22 01:09

timotimo


try something();
fail $! if $!;

Note that CATCH blocks apply to all statements in the same scope, even to code after the CATCH block. So if you want to use CATCH blocks, be careful about keeping the scope small.

like image 42
moritz Avatar answered Sep 17 '22 01:09

moritz