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

Re: Try/Catch Exception Objects: Possible?

Thread Previous | Thread Next
From:
Aristotle Pagaltzis
Date:
July 23, 2013 22:05
Subject:
Re: Try/Catch Exception Objects: Possible?
Message ID:
20130723220516.GA6739@fernweh.plasmasturm.org
* David E. Wheeler <david@kineticode.com> [2013-07-23 22:00]:
> The catch block should not do conditional execution. One should use
> a switch statement or whatever inside the catch block.

That means if you want to take only the exceptions you understand how to
handle, then a) you need another level of nesting and b) you need to add
boilerplate to rethrow the exception.

That is exactly what grates on me when I use Try::Tiny. But I don’t hold
that against it: Perl simply does not provide the means to sanely offer
better as a module.

There is no reason core syntax cannot improve on this, though.

Now I’m not keen on some new ad-hoc type of smartmatch just because it
would be similar to what other languages offer which already have some
try/catch construct. So I’d just have `catch` follow the same syntax as
any other kind of conditional in Perl:

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

You’d be able to chain multiple `catch`es, of course.

If none of the `catch` conditions are true, the exception goes through
uncaught. So in the given example, transient errors would be caught, but
any other kind of exception would go right back to unwinding the stack
as though no `try` had been there.

You say `catch (1) {...}`, obviously, if you really do want to catch ’em
all. Which I like, because it a) forces consciousness of your choice on
you when you write that and b) throws up a very obvious flag for anyone
skimming the code (or, maybe, allows *other* kinds of `catch` to *lower*
the flag).

Regards,
-- 
Aristotle Pagaltzis // <http://plasmasturm.org/>

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