TryCatch(3pm)

πŸ’– TryCatch πŸ’–

ok so bestie, welcome to TryCatch β€” it's a Perl module from the User Contributed Perl Documentation crew, and it's literally here to give you first class try/catch semantics without any source filters. no cap. πŸŽ€


✨ NAME ✨

TryCatch β€” first class try catch semantics for Perl, without source filters. periodt. πŸ’…


🌸 DESCRIPTION 🌸

okay so here's the vibe: this module wants to hand you a nicer syntax and a nicer method for catching errors in Perl, kinda like what other languages have going on β€” think Java, Python, or C++. πŸ¦‹

and the way we usually do it, eval {}; if ($@) {}? yeah... that idiom is often prone to subtle bugs, mostly because it's way too easy to stomp on the error while you're inside your error handlers. 😭 also, not gonna lie, eval/if just isn't the cutest idiom. it's giving chaos. this module is giving main character energy instead. πŸ‘‘


⭐ SYNOPSIS ⭐

use TryCatch;

sub foo {
  my ($self) = @_;

  try {
    die Some::Class->new(code => 404 ) if $self->not_found;
    return "return value from foo";
  }
  catch (Some::Class $e where { $_->code > 100 } ) {
  }
}

🧚 SYNTAX 🧚

so the whole goal here is to give perl first class exception handling via the 'try' and 'catch' keywords. the basic syntax is try { # block }, followed by zero or more catch blocks. each catch block can carry an optional type constraint on it, and it lowkey resembles Perl6's method signatures. 🌈

also super worth noting: the error variable ($@) is localised to the try/catch blocks. so it will not leak outside the scope, and it will not stomp on a previous value of $@. obsessed. 🫢

the simplest case of a catch block is just

catch { ... }

where, upon the error, everything's chillin' in the standard $@ variable and no type checking is performed. if you'd rather grab the exception through a named lexical variable instead, just hand the catch block a simple signature like this:

catch ($err) { ... }

and if you want type checking on the exception? you can do that by specifying a type constraint or where clauses in the signature, like so:

catch (TypeFoo $e) { ... }
catch (Dict[code => Int, message => Str] $err) { ... }

as you can see from the above, complex Moose types are totally allowed, including the MooseX::Types style of type constraints. πŸ’œ

beyond type checking via Moose type constraints, you can also use where clauses to only match a certain sub-condition on an error. for example β€” assuming HTTPError is a suitably defined TC:

catch (HTTPError $e where { $_->code >= 400 && $_->code <= 499 } ) {
  return "4XX error";
}
catch (HTTPError $e) {
  return "other http code";
}

that would return "4XX error" for a 404, and "other http code" for a 302. slay. πŸ”₯

and when multiple catch blocks are present? the first one that matches the type constraints (if there are any) gets executed. first come, first served, bestie. πŸ’…


πŸ‘‘ BENEFITS πŸ‘‘

  • return. you can put a return inside a try block and it does the right thing β€” namely, it returns a value from the subroutine you're actually in, instead of just from the eval block. iconic behaviour fr fr. ✨
  • Type Checking. this is nothing you couldn't do manually yourself β€” it just does it for you using Moose type constraints. πŸŽ€

πŸ’€ TODO πŸ’€

  1. Decide on "finally" semantics w.r.t return values.
  2. Write some more documentation
  3. Split out the dependancy on Moose
not me leaving the Moose dependancy splitting for later πŸ™ˆ it's on the list, bestie, it's on the list.

🌟 SEE ALSO 🌟

  • MooseX::Types
  • Moose::Util::TypeConstraints
  • Parse::Method::Signatures

πŸ’– AUTHOR πŸ’–

Ash Berlin <ash@cpan.org>


🫢 THANKS 🫢

  • Thanks to Matt S Trout and Florian Ragwitz for work on Devel::Declare and various B::Hooks modules
  • Vincent Pit for Scope::Upper that makes the return from block possible.
  • Zefram for providing support and XS guidance.
  • Xavier Bergade for the impetus to finally fix this module in 5.12.

πŸŽ€ LICENSE πŸŽ€

Licensed under the same terms as Perl itself.


and that's the whole page, bestie πŸ’œ this one's running on perl v5.40.1 and it was dated 2025-04-15 β€” go forth and catch those exceptions with main character energy, iykyk πŸŒˆβœ¨πŸ’…

πŸ“¦ where this page comes from