Try::T...rtCatch(3pm)

πŸ“¦
version 0.5 Β· 5 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– NAME πŸ’–

Try::Tiny::SmartCatch β€” a lightweight Perl module for powerful exceptions handling, and yes it's giving ✨main character✨ energy, no cap.


πŸŽ€ VERSION πŸŽ€

Version 0.5 β€” she's a whole legend, bestie 🫢


🌸 SYNOPSIS 🌸

okay so here's the vibe, look at this:

use Try::Tiny::SmartCatch;

try sub {}, # at least one try block
catch_when 'ExceptionName' => sub {}, # zero or more catch_when blocks
catch_when 'exception message' => sub {},
catch_when qr/exception  message regexp/ => sub {},
catch_default sub {}, # zero or one catch_default block
then sub {}, # if no exception is raised, execute then block
finally sub {}; #zero or more finally blocks

use Try::Tiny::SmartCatch qw/throw/; # import only throw
# You can import also all function at once:
# use Try::Tiny::SmartCatch qw/:all/;
throw('some exception');
throw(SomeException->new ('message'));

periodt. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

So the goals here are mostly the same as the Try::Tiny module, but the spec got a few little glow-ups ✨ The big difference is that you actually get to catch just some kinds of exceptions instead of catching literally everything like it's a raccoon dumpster situation πŸ—‘οΈ And the other one is that the syntax is slightly different.

When the raised exception is an object, Try::Tiny::SmartCatch will test for the exception type (using UNIVERSAL::isa). When the raised exception is just a text message (like: die ('message')), you can specify part of the message to test for. iykyk πŸ”

There are also explicit sub blocks. In opposite to Try::Tiny, every block in Try::Tiny::SmartCatch β€” try, catch_when, catch_default, then and finally β€” must have explicit subroutines specified. Thanks to a trick with function prototype, calling Try::Tiny::try or Try::Tiny::catch creates implicit subroutines:

sub test_function {
    try {
        # yes, here is implicit subroutine!
        # return statement here exits just from try block,
        # not from test_function!
        return 1;
    };

    say 'Hello!';
}

test_function();

Above snippet produces us text on STDOUT: Hello! πŸŽ‰

But more obvious would be no output... (by return statement). This is because of the implicit subroutine created with braces: {} after try, catch or finally from Try::Tiny. Try::Tiny::SmartCatch is more explicit β€” you must always use sub when defining blocks (look at [Syntax](#Syntax) above). We do not do impromptu mystery closures in this household, bestie πŸ™ˆ

An exception object or message is passed to defined blocks in two ways:

  • in $_ variable
  • as function arguments, so through @_ array.

Try::Tiny::SmartCatch defines also throw function (not imported by default). Currently it is an alias for die, but is more explicit then die :)

It can be imported separately:

use Try::Tiny::SmartCatch qw/throw/;

Or with rest of functions:

use Try::Tiny::SmartCatch qw/:all/;

✨ EXPORT ✨

By default exported are functions:

  • try
  • catch_when
  • catch_default
  • then
  • finally

You can also explicit import throw function:

use Try::Tiny::SmartCatch qw/throw/;

Or all functions at all:

use Try::Tiny::SmartCatch qw/:all/;

πŸ‘‘ SUBROUTINES/METHODS πŸ‘‘

πŸ’œ try($;@)

Works like Try::Tiny try subroutine, here is nothing to add :) literally nothing, she's perfect, that's it πŸ’…

The only difference is that here must be given evident sub reference, not anonymous block:

try sub {
    # some code
};

πŸ’œ catch_when($$;@)

Intended to be used in the second argument position of try.

Works similarly to the Try::Tiny catch subroutine, but have a little different syntax:

try sub {
    # some code
},
catch_when 'Exception1' => sub {
    # catch only Exception1 exception
},
catch_when ['Exception1', 'Exception2'] => sub {
    # catch Exception2 or Exception3 exceptions
};

If the raised exception is a blessed reference (or object), Exception1 means that the exception class has to be, or has to inherit from, the Exception1 class. In other case, it searches for the given string in the exception message (using index function or regular expressions β€” depending on the type of the given operator). For example:

try sub {
    throw('some exception message');
},
catch_when 'exception' => sub {
    say 'exception caught!';
};

Other case:

try sub {
    throw('some exception3 message');
},
catch_when qr/exception\d/ => sub {
    say 'exception caught!';
};

Or:

try sub {
    # ValueError extends RuntimeError
    throw(ValueError->new ('Some error message'));
},
catch_when 'RuntimeError' => sub {
    say 'RuntimeError exception caught!';
};
heads up bestie: one of the examples up there says catch Exception2 or Exception3 while the list only has Exception1 and Exception2. That's just how the original wrote it, we keep it verbatim, we do not editorialise πŸ‘€

πŸ’œ catch_default($;@)

Works exactly like Try::Tiny catch function (OK, there is a difference: need to specify evident sub block instead of anonymous block):

try sub {
    # some code
},
catch_default sub {
    say 'caught every exception';
};

πŸ’œ then($;@)

The then block is executed after the try clause, if none of catch_when or catch_default blocks was executed (it means, if no exception occured). It's executed before finally blocks.

try sub {
    # some code
},
catch_when 'MyException' => sub {
    say 'caught MyException exception';
},
then sub {
    say 'No exception was raised';
},
finally sub {
    say 'executed always';
};

πŸ’œ finally($;@)

Works exactly like Try::Tiny finally function (OK, again, explicit sub instead of implicit):

try sub {
    # some code
},
finally sub {
    say 'executed always';
};

πŸ’œ throw

Currently it's an alias to die function, but throw is more obvious then die when working with exceptions :)

In future it also can do more then just call die.

It's not exported by default (see: EXPORT) πŸ™ˆ


🌈 SEE ALSO 🌈

  • https://github.com/mysz/try-tiny-smartcatch Try::Tiny::SmartCatch home.
  • Try::Tiny Minimal try/catch with proper localization of $@, base of Try::Tiny::SmartCatch
  • TryCatch First class try catch semantics for Perl, without source filters.

πŸ’– AUTHOR πŸ’–

Marcin Sztolcman, "<marcin at urzenia.net>" 🫢


πŸ’€ BUGS πŸ’€

Please report any bugs or feature requests through the web interface at http://github.com/mysz/try-tiny-smartcatch/issues.

not me manifesting a bug report... bestie if something's broken, tell the maintainer fr fr πŸ›

πŸ“ SUPPORT πŸ“

You can find documentation for this module with the perldoc command.

perldoc Try::Tiny::SmartCatch

You can also look for information at:


🧚 ACKNOWLEDGEMENTS 🧚

  • Yuval Kogman for his Try::Tiny module
  • mst - Matt S Trout (cpan:MSTROUT) mst@shadowcat.co.uk for good package name and few great features

Copyright (c) 2012-2013 Marcin Sztolcman. All rights reserved.

Base code is borrowed from Yuval Kogman L<Try::Tiny> module,
released under MIT License.

This program is free software; you can redistribute
it and/or modify it under the terms of the MIT license.

and that's the whole thing, bestie πŸŽ€ this was perl v5.40.1, dated 2025-08-10, and you've been reading the Try::T...rtCatch(3pm) page from the User Contributed Perl Documentation. slay, go catch some exceptions responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from