Try::Tiny(3p)

NAME
Try::Tiny - minimal try/catch with proper localization of $@

SYNOPSIS
        # handle errors with a catch handler
        try {
                die "foo";
        } catch {
                warn "caught error: $_";
        };

        # just silence errors
        try {
                die "foo";
        };

DESCRIPTION
This module provides bare bones "try"/"catch" 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).

EXPORTS
All functions are exported by default using Exporter.

In the future Sub::Exporter may be used to allow the keywords to be renamed, but this technically does not satisfy Adam Kennedy's definition of "Tiny".

try (&;$)
    Takes one mandatory try subroutine and one optional catch subroutine.

    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..

catch (&)
    Intended to be used in the second argument position of "try".

    Just returns the subroutine it was given.

            catch { ... }

    is the same as

            sub { ... }

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.

  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 an overloaded error object that evaluates to false, but that's asking for trouble anyway.

The classic failure mode is:

        sub Object::DESTROY {
                eval { ... }
        }

        eval {
                my $obj = Object->new;

                die "foo";
        };

        if ( $@ ) {

        }

In this case since "Object::DESTROY" is not localizing $@ but still uses "eval" it will set $@ to "".

The destructor is called when the stack is unwound, after "die" sets $@ to "foo at Foo.pm line 42\n", so by the time "if ( $@ )" is evaluated it has been cleared by "eval" in the destructor.

The workaround for this is even uglier than the previous ones. Even though we can't save the value of $@ from code that doesn't localize, we can at least be sure the eval was aborted due to an error:

        my $failed = not eval {
                ...

                return 1;
        };

This is because an "eval" that caught a "die" will always return a false value.

SHINY SYNTAX
Using Perl 5.10 you can use "Switch statements" in perlsyn.

The "catch" block is invoked in a topicalizer context (like a "given" block), but note that you can't return a useful value from "catch" using the "when" blocks without an explicit "return".

This is somewhat similar to Perl 6's "CATCH" blocks. You can use it to concisely match errors:

        try {
                require Foo;
        } catch {
                when (/^Can't locate .*?\.pm in \@INC/) { } # ignore
                default { die $_ }
        };

CAVEATS

•   "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 preserved, there is no safe way to ensure this if "eval" is used unhygenically in destructors. It's only guaranteeed that the "catch" will be called.

SEE ALSO

TryCatch
    Much more feature complete, more convenient semantics, but at the cost of implementation complexity.

autodie
    Automatic error throwing for builtin functions and more. Also designed to work well with "given"/"when".

Throwable
    A lightweight role for rolling your own exception classes.

Error
    Exception object implementation with a "try" statement. Does not localize $@.

Exception::Class::TryCatch
    Provides a "catch" statement, but properly calling "eval" is your responsibility.

    The "try" keyword pushes $@ onto an error stack, avoiding some of the issues with $@ but you still need to localize to prevent clobbering.

VERSION CONTROL
<http://github.com/nothingmuch/try-tiny/>

AUTHOR
Yuval Kogman <nothingmuch@woobling.org>

COPYRIGHT
        Copyright (c) 2009 Yuval Kogman. All rights reserved.
        This program is free software; you can redistribute
        it and/or modify it under the terms of the MIT license.