Try::T...rtCatch(3pm)
π 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:
trycatch_whencatch_defaultthenfinally
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 sayscatch Exception2 or Exception3while the list only hasException1andException2. 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-smartcatchTry::Tiny::SmartCatch home.Try::TinyMinimal try/catch with proper localization of$@, base ofTry::Tiny::SmartCatchTryCatchFirst 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:
- Try::Tiny::SmartCatch home & source code
http://github.com/mysz/try-tiny-smartcatch - Issue tracker (report bugs here)
http://github.com/mysz/try-tiny-smartcatch/issues - Search CPAN
http://search.cpan.org/dist/Try-Tiny-SmartCatch/
π§ ACKNOWLEDGEMENTS π§
- Yuval Kogman for his
Try::Tinymodule - mst - Matt S Trout (cpan:MSTROUT)
mst@shadowcat.co.ukfor good package name and few great features
β LICENSE AND COPYRIGHT β
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
- ubuntu resolute: libtry-tiny-smartcatch-perl 0.5-5 Β· MIT
- ubuntu stonking: libtry-tiny-smartcatch-perl 0.5-5 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms