develooper Front page | perl.perl5.porters | Postings from July 2013

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
From:
David Golden
Date:
July 23, 2013 22:08
Subject:
Re: Try/Catch Exception Objects: Possible?
Message ID:
CAOeq1c-P4vc4EjmWpROd=kB6DcA8dbYr8bSc7Yb-x6UeTwpg1w@mail.gmail.com
On Tue, Jul 23, 2013 at 3:56 PM, David E. Wheeler <david@kineticode.com> wrote:
> The catch block should not do conditional execution. One should use a switch statement or whatever inside the catch block.

Why not? Please make a real case for that opinion.  If the common case
is going to be tests for certain types of exceptions or a given/when
block, why wouldn't we want to provide sugar for that?  (Which is why
my playful example had "catch CONDITION" and "otherwise BLOCK".)

> Might be nice to pass the error, but no other block works that way (only anonymous subs), so using a lexical $_ would probably be the way to go (again, like Try::Tiny).

Do you mean "localized $_" instead of "lexical $_"?  Didn't we just
decide that lexical $_ is a problem?  Even so, why should we continue
to perpetuate the use of globals which can be modified by code called
by an error handler?

How about using a non-argument lexical like "for" blocks:

   for my $foo ( list() ) { ... }

For try/catch:

   try { ... }
   catch my $err { ... }

And like "for", omit "my $err" to have the exception in "$_".

If we went for omitting conditional logic from catch clauses, then
that sort of shortcut seems useful.

    try {
        risky();
    }
    catch my $err {
        ...
    }


David


-- 
David Golden <xdg@xdg.me>
Take back your inbox! → http://www.bunchmail.com/
Twitter/IRC: @xdg

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About