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
From:
Paul "LeoNerd" Evans
Date:
June 27, 2020 18:17
Subject:
Re: Thoughts in the direction of a core try/catch syntax
Message ID:
20200627191658.46f25a13@shy.leonerd.org.uk
On Sat, 27 Jun 2020 10:47:40 -0700
Karen Etheridge <perl@froods.org> 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

leonerd@leonerd.org.uk      |  https://metacpan.org/author/PEVANS
http://www.leonerd.org.uk/  |  https://www.tindie.com/stores/leonerd/

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