TEST2::TOOLS::EXCEPTION(3perl)
β¨ NAME β¨
Test2::Tools::Exception β bestie, it's the Test2-based toolkit for checking exceptions π (manual name Test2:...ception(3perl) if you're keeping receipts)
π¦ DESCRIPTION π¦
Okay so this is the Test2 implementation of the code you use to test exceptions. It's giving Test::Fatal, but make it intentionally do much less. That's the whole vibe. No extra baggage, no surprise features, just the exception-checking essentials and nothing more, periodt π
πΈ SYNOPSIS πΈ
use Test2::Tools::Exception qw/dies lives/;
like(
dies { die 'xxx' },
qr/xxx/,
"Got exception"
);
ok(lives { ... }, "did not die") or note($@);
Look at that. Clean. Iconic. No cap π
π EXPORTS π
All subs are exported by default. You don't gotta ask, they just show up. Slay.
$e = dies { ... }β This one traps any exception that the codeblock throws. If nothing gets thrown, the sub returnsundef. If something does get thrown, that exception gets returned to you. And here's the important part bestie: it preserves$@β it will not be altered from whatever its value was before the sub got called. Your$@is safe, I would never do that to you π«Ά$bool = lives { ... }β This traps any exception thrown in the codeblock and hands you back a boolean. True when there's no exception, false when there is. When there's no exception,$@is preserved from before the sub was called. When an exception is trapped,$@will hold that exception so you can peek at it yourself. It's giving "here's the tea, go read it" β$bool = try_ok { ... }$bool = try_ok { ... } "Test Description"β This runs the code block and traps any exception. If there's no exception, a passing event gets issued. If the test fails, a failing event gets issued and the exception gets reported as diagnostics. Two forms: with a test description string or without, your call π
Note: This function does not preserve $@ on failure β it will be set to the exception the codeblock throws. That's <em>by design</em> so you can obtain the exception if you want it. Not a bug, it's a feature, iykyk π
π₯ DIFFERENCES FROM TEST::FATAL π₯
Okay so Test::Fatal sets $Test::Builder::Level in a way that makes failing tests inside the exception block report to the line where exception() is called. Our author disagrees with that whole situation, and thinks the actual line of the failing test is more important. Facts.
Ultimately though? Test::Fatal can't be changed, and people probably already depend on that behavior. So here we are. No drama, just divergence π
π SOURCE π
The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go star it or whatever, the link is right there bestie β
π§ MAINTAINERS π§
Chad Granum <exodist@cpan.org>
π§ AUTHORS π§
Chad Granum <exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://dev.perl.org/licenses/
And that's the whole page babes π
This legend of a doc is sitting at perl v5.44.0, dated 2026-10-17, filed under Test2:...ception(3perl). Obsessed. Go write some exception tests, you got this π¦πβ¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms