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

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
David E. Wheeler
July 25, 2013 10:42
Re: Try/Catch Exception Objects: Possible?
Message ID:

On Jul 24, 2013, at 12:07 AM, David Golden <> wrote:

> On Tue, Jul 23, 2013 at 3:56 PM, David E. Wheeler <> 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.

The case is just that we should try to focus on fundamentals, first. This sort of thing can be added once the fundamentals work. IOW, if we prove that core exception objects will work without unduly breaking CPAN, and that we can do proper scoping with try/catch (or try/CATCH), then it might be worth considering adding conditionals (I kind of like Aristotle’s suggestion). But we do not have to do that up-front.

> 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".)

We might, but do not need to start there.

>> 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 $_"?

I do not.

> Didn't we just decide that lexical $_ is a problem?

I was not sure where that was left. Is it dead now? I know it was removed from some contexts (grep and map?) in 5.18, but this is an entirely new OP I’m talking about, so there would be no legacy to work around.

> 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() ) { ... }

IIRC, Chip apologized for this syntax, because it’s declared outside the block yet lexically defined inside the block. Is it really something we want to spread?

> For try/catch:
>   try { ... }
>   catch my $err { ... }
> And like "for", omit "my $err" to have the exception in "$_".

Could you do conditional checks for each catch that way, as Aristotle suggested? Would it be defined inside the parens, as in:

   my $retry = 0;
   while ( $retry < 5 ) {
       try { $some->transaction }
       catch my $err ( $err->isa('X::Transient') ) { ++$retry }

Seems a *little* weird, but not too bad. I guess global $_ would be the default just like for?



Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About