develooper Front page | perl.perl5.porters | Postings from June 2020

Re: Thoughts in the direction of a core try/catch syntax

Thread Previous | Thread Next
Paul "LeoNerd" Evans
June 27, 2020 18:17
Re: Thoughts in the direction of a core try/catch syntax
Message ID:
On Sat, 27 Jun 2020 10:47:40 -0700
Karen Etheridge <> wrote:

> catch my $e {
>   if ($e isa X::SomeException) { ... }
>   elsif ($e =~ m/^Cannot do a thing/) { ... }
>   else { ... }

Ah; but this "..." is exactly the part I'm interested in. What "..."
code do you put there?

If there was a syntax for core's catch mechanism to realise that you
weren't interested in this particular exception, then it can keep
unwinding the stack until it finds a different set of try/catch blocks
from some higher-up caller to invoke instead.

This is why I would really like a core try/catch to have typed
dispatch. It is not *simply* to assist you not having to do the
if/elsif dispatch yourself. It adds a whole new semantic ability of
*not* catching an exception and letting your caller handle it instead.

  try { someweirdfunc() }
  catch (isa X::MyOwnExceptionType) { ... }

If someweirdfunc throws anyone else's exception object, or not an
exception object at all but just a bare string, the code behaves as if
this `try` never saw it. We keep just propagating up to the caller.

Imagine never having to type

  else { die $@ }

ever again. We don't want to re-throw exceptions, we want to not catch
them in the first place.

Paul "LeoNerd" Evans      |  |

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