Hi

this is part of a longer meditation, but I want to keep it short:

I want to be able to catch error-objects thrown with die and I want to have an easy syntax:

the JS model is reproducible 1-to-1 with Try::Tiny it's even easier because Perl's $_ is automatically set to JS's e

from try...catch#conditional_catch-blocks

try { myroutine(); // may throw three types of exceptions } catch (e) { if (e instanceof TypeError) { // statements to handle TypeError exceptions } else if (e instanceof RangeError) { // statements to handle RangeError exceptions } else if (e instanceof EvalError) { // statements to handle EvalError exceptions } else { // statements to handle any unspecified exceptions logMyErrors(e); // pass exception object to error handler } }

But in most cases the final else will just do die $_ to propagate the error to higher call levels, hence it's boilerplate.

Python has a model for this by providing a condition after the 'catch', if non is matched the error is raised again.

from https://pythonbasics.org/try-except/

try: # your code here except FileNotFoundError: # handle exception except IsADirectoryError: # handle exception except: # * # all other types of exceptions print('Should reach here')

(* the last except must be removed to automatically raise the error again)

So ideally one could define in Perl a prototype for catch (COND) { CODE } where the (COND) part is optional. Alas that's not possible in Perl, even with mandatory (COND) (otherwise experiments with syntax extension were easy)

Three workarounds come into mind

1. a bail out function only {} (or maybe handle {} )

try { # your code here } catch { only { FileNotFoundError }; # ° # handle exception } catch { only { IsADirectoryError }; # handle exception } catch { # * # all other types of exceptions }; print('Should reach here')

(* again, if the simple catch is removed the error is automatically thrown again die $_ )

( ° the COND is either a boolean expression or a constant resp. object representing an error-class. Since an error-class is blessed into a type like "ErrorClass" this can be tested by only ... hence a shorthand for $_->isa(FileNotFoundError) ... not sure if Python has the same flexibility ;)

2. extend catch (&;&) {...} with optional second sub {}

try { # your code here } catch { FileNotFoundError } sub { # handle exception }, catch { IsADirectoryError } sub { # handle exception }, catch { # * # all other types of exceptions }; print('Should reach here')

UPDATE: added commas, plz see here why.

3. as a variant of 2. use an "underscore" sub _ as syntactic sugar to chain code-blocks

with sub _ (&;@){ return @_ }

try { # your code here } catch { FileNotFoundError }_ { # handle exception } catch { IsADirectoryError }_ { # handle exception } catch { # * # all other types of exceptions }; print('Should reach here')

This variant could help implementing a future built-in syntax catch () {} by parsing {...}_ as (...)

Like that module authors wanting to be backwards compatible to older versions could keep writing {...}_ without loosing the full performance of newer versions.

And new syntax for compound statements could be experimentally implemented and tested.

finally

I wanted to keep it short and didn't show much implementation detail. I'm more interested in comments regarding the interface... or probably I'm missing a good CPAN module already?

comments? suggestions?

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to conditional catch-blocks 'try {} catch(COND) { }' by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.