SYNOPSIS

DESCRIPTION

This module provides bare bones try/catch/finally statements that are designed to minimize common mistakes with eval blocks, and NOTHING else.

This is unlike TryCatch which provides a nice syntax and avoids adding another call stack layer, and supports calling return from the try block to return from the parent subroutine. These extra features come at a cost of a few dependencies, namely Devel::Declare and Scope::Upper which are occasionally problematic, and the additional catch filtering uses Moose type constraints which may not be desirable either.

The main focus of this module is to provide simple and reliable error handling for those having a hard time installing TryCatch, but who still want to write correct eval blocks without 5 lines of boilerplate each time.

It's designed to work as correctly as possible in light of the various pathological edge cases (see BACKGROUND) and to be compatible with any style of error values (simple strings, references, objects, overloaded objects, etc).

If the try block dies, it returns the value of the last statement executed in the catch block, if there is one. Otherwise, it returns undef in scalar context or the empty list in list context. The following two examples both assign "bar" to $x.

The mandatory subroutine is evaluated in the context of an eval block.

If no error occurred the value from the first block is returned, preserving list/scalar context.

If there was an error and the second subroutine was given it will be invoked with the error in $_ (localized) and as that block's first and only argument.

Note that the error may be false, but if that happens the catch block will still be invoked.

Once all execution is finished then the finally block if given will execute.

catch (&;$)

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

Returns a reference to the subroutine it was given but blessed as Try::Tiny::Catch which allows try to decode correctly what to do with this code reference.

catch { ... }

Inside the catch block the previous value of $@ is still available for use. This value may or may not be meaningful depending on what happened before the try, but it might be a good idea to preserve it in an error stack.

finally (&;$)

try { ... }
catch { ... }
finally { ... };

Or

try { ... }
finally { ... };

Or even

try { ... }
finally { ... }
catch { ... };

Intended to be the second or third element of try. Finally blocks are always executed in the event of a successful try or if catch is run. This allows you to locate cleanup code which cannot be done via local() e.g. closing a file handle.

You must always do your own error handling in the finally block. Try::Tiny will not do anything about handling possible errors coming from code located in these blocks.

In the same way catch() blesses the code reference this subroutine does the same except it bless them as Try::Tiny::Finally.

BACKGROUND

There are a number of issues with eval.

Clobbering $@

When you run an eval block and it succeeds, $@ will be cleared, potentially clobbering an error that is currently being caught.

This causes action at a distance, clearing previous errors your caller may have not yet handled.

$@ must be properly localized before invoking eval in order to avoid this issue.

More specifically, $@ is clobbered at the begining of the eval, which also makes it impossible to capture the previous error before you die (for instance when making exception objects with error stacks).

For this reason try will actually set $@ to its previous value (before the localization) in the beginning of the eval block.

Localizing $@ silently masks errors

Inside an eval block die behaves sort of like:

sub die {
$@ = $_[0];
return_undef_from_eval();
}

This means that if you were polite and localized $@ you can't die in that scope, or your error will be discarded (printing "Something's wrong" instead).

The workaround is very ugly:

my $error = do {
local $@;
eval { ... };
$@;
};
...
die $error;

$@ might not be a true value

This code is wrong:

if ( $@ ) {
...
}

because due to the previous caveats it may have been unset.

$@ could also be an overloaded error object that evaluates to false, but that's asking for trouble anyway.

CAVEATS

return returns from the try block, not from the parent sub (note that this is also how eval works, but not how TryCatch works):

sub bar {
try { return "foo" };
return "baz";
}
say bar(); # "baz"

try introduces another caller stack frame. Sub::Uplevel is not used. Carp will report this when using full stack traces. This lack of magic is considered a feature.

The value of $_ in the catch block is not guaranteed to be the value of the exception thrown ($@) in the try block. There is no safe way to ensure this, since eval may be used unhygenically in destructors. The only guarantee is that the catch will be called if an exception is thrown.

The return value of the catch block is not ignored, so if testing the result of the expression for truth on success, be sure to return a false value from the catch block:

As a valued partner and proud supporter of MetaCPAN, StickerYou is
happy to offer a 10% discount on all Custom Stickers,
Business Labels, Roll Labels,
Vinyl Lettering or Custom Decals. StickerYou.com
is your one-stop shop to make your business stick.
Use code METACPAN10 at checkout to apply your discount.