in reply to Exception clauses

Hello betmatt

you can use Try::Tiny or Syntax::Keyword::Try module. From the synopis of the modules:

use Try::Tiny; # handle errors with a catch handler try { die "foo"; } catch { warn "caught error: $_"; # not $@ }; # or use Syntax::Keyword::Try; sub foo { try { attempt_a_thing(); return "success"; } catch { warn "It failed - $@"; return "failure"; } }

Also look at ModernPerl book in the chapter dedicated to Exceptions.

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Replies are listed 'Best First'.
Re^2: Exception clauses
by bliako (Abbot) on Jun 06, 2019 at 10:24 UTC

    Discipulus just to add that there exists a finally clause for both modules you mentioned. Here is how to do it with Try::Tiny:

    try { try_some_code() } catch { warn "oops: $_" } finally { $x = 'always-executes' };

    btw, I was bitten many times by forgetting the last colon and getting all sort of misleading error messages. This is not java!