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

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
From:
David Nicol
Date:
July 24, 2013 02:41
Subject:
Re: Try/Catch Exception Objects: Possible?
Message ID:
CAFwScO9ZP3p9QqQg2Mdifx10ohMqdFg-w73WX+h2=ScSS2sHzw@mail.gmail.com
On Tue, Jul 23, 2013 at 2:04 PM, David Golden <xdg@xdg.me> wrote:
>
> that isa 'FOO').  However, if the exception needs to be parked in a
> global, I'd park it in $@ for consistency with what happens outside
> try/catch.


I nominate @@ for the stack of new exceptions, for consistency without
interference.

A "Yoda's exception system" (in which, drumroll please, there is no
try) can be effected
with a DEFER block.

C< catch FooE { ... } >  might essentially expand into something like

     OnScopeExit {  # before, after, or during reassignment of locals?
             my @CatchMe, @NewStack;
             push @{   $_->isa('FooE') ? \@CatchMe : \@NewStack }, $_ for @@;
             for ( @CatchMe ) {
                    ...
             };
             @@ = @NewStack;  #  any advantage to *@ = \@NewStack  ???
     }


Why a stack, instead of just one variable? To handle situations where
deferred code throws another exception, as well as exceptions thrown
from DESTROY routines, thus
avoiding the problems caused clearing an unlocalized $@.

The big difference between $@ and the above-proposed @@ has to do with
clearing it: $@ gets cleared on all C<eval> calls; @@ would only get
items removed from it when they
are explicitly consumed, either by convention or by restricting access to it.

Further compatibility could be provided with

    sub YodaTry(&) {
            eval { shift ->() ; 1 } or push @@, $@
    }

I see how one could create a pure-perl YodaCatch, using the hack from
Scope::local_OnExit, but the syntax would wind up looking like

      local @YodaCatch = ( FooE => sub { ... }, );

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