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

Re: Core exception types [was: Re: Pre-RFC: Improve “wide character” warnings]

Thread Previous | Thread Next
From:
Felipe Gasper
Date:
June 8, 2022 12:39
Subject:
Re: Core exception types [was: Re: Pre-RFC: Improve “wide character” warnings]
Message ID:
D561C38F-1464-4180-998E-39C9AACB6721@felipegasper.com


> On Jun 8, 2022, at 07:12, Paul LeoNerd Evans <leonerd@leonerd.org.uk> wrote:
> 
>  use builtin 'blessed';
> 
>  try {
>    maybe_call_a_func();
>  }
>  catch ($e) {
>    if(blessed $e) { 
>      warn "Caught a user-defined exception of type " . (blessed $e);
>    }
>    else {
>      warn "Caught a plain stringy exception";
>    }
>  }
> 
> What should we do here?
> 
> 1) Throw objects of some core-defined type, so `ref` and `blessed` are
>    now true on these things, meaning they can be distinguished -
>    including by some sort of `isa` test as might someday be added to
>    `catch`, but thus breaking all existing code which inspects $@ or
>    $e.

Worth clarifying: this won’t “break all existing code which inspects $@ or $e”; it would only break code that expects exception objects to be user-thrown errors.

(I know, of course, that you know that, but IMO it’s worth being specific about the scope.)

FWIW, I’ve never written such code. Whenever I care about an error object’s blessed-ness, I check its class. So I’d write, e.g.:

-----
try {
   throw_object();
}
catch ($e isa Some::TLS::X::Base) {
   # It’s a TLS-level error
}
catch ($e isa IO::Socket::X::Base) {
   # It’s a socket-level error
}
catch ($e) {
   warn "Some unhandled error: $e";
}
-----

I can’t speak for what others may have written, of course.

> 2) Throw plain strings that have some other, new way to query some
>    hidden "error type" information stored within them. Maybe lets
>    invent some new `builtin` funcs and imagine a hypothetical future
> 
>      use builtin 'extype';
> 
>      ...
>      catch($e) {
>        my $t = extype $e;
>        if($t eq "SOMETHING") { ... }
>        elsif ...
>      }

Attaching metadata to plain scalars could be powerful. It feels like it would encourage “weirdness”, but I haven’t researched it. I know decorator-type things are a common feature in other languages.

-FG

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