On 11/24/2015 06:39 AM, brad clawsie wrote:
> Been playing with perl6 and it is truly amazing.
> I'm somewhat confused as to when I should should wrap subroutine
> invocations with `try`. My `CATCH` clauses seem to be able to catch
> thrown Exception instances if I use `try` or not.

That's correct. "try" is merely a convience for catching all exceptions,
and not needed when you have an explicit CATCH block.

> I see in the
> "Exceptional Perl 6" slide deck
> ( http://moritz.faui2k3.org/files/talks/2013-yapc-p6-exceptions/ ),
> there seems to be an indication that `try` should be used when I want to
> access a Backtrace...is there something here I am missing?

The exception object always gives you access to the backtrace. If you
use CATCH, then the exception is in $_ inside the CATCh block. If you
use try, the exception is in $! outside the try block.

Here are two examples that do the same thing:

try { die "oh noez" };
say $!.message;             # oh noez
say $!.^name;               # X::AdHoc

do {
    die "oh noez";
    CATCH {
        default {
            say .message;   # oh noez
            say .^name;     # X::AdHoc

Why bother with the more verbose form at all? There are basically two
1) you can chose to only catch some exceptions, like
EVAL $some-code;
   when X::Comp {  say "Your code didn't even compile" };
   # let run-time exceptions fall through

2) When you want to access some variables from the inner scope that
produced the exception. Since CATCH runs before the stack is unwound,
you can even inspect dynamic variables.


Reply via email to