1TryCatch(3)           User Contributed Perl Documentation          TryCatch(3)
2
3
4

NAME

6       TryCatch - first class try catch semantics for Perl, without source
7       filters.
8

DESCRIPTION

10       This module aims to provide a nicer syntax and method to catch errors
11       in Perl, similar to what is found in other languages (such as Java,
12       Python or C++).  The standard method of using "eval {}; if ($@) {}" is
13       often prone to subtle bugs, primarily that its far too easy to stomp on
14       the error in error handlers.  And also eval/if isn't the nicest idiom.
15

SYNOPSIS

17        use TryCatch;
18
19        sub foo {
20          my ($self) = @_;
21
22          try {
23            die Some::Class->new(code => 404 ) if $self->not_found;
24            return "return value from foo";
25          }
26          catch (Some::Class $e where { $_->code > 100 } ) {
27          }
28        }
29

SYNTAX

31       This module aims to give first class exception handling to perl via
32       'try' and 'catch' keywords. The basic syntax this module provides is
33       "try { # block }" followed by zero or more catch blocks. Each catch
34       block has an optional type constraint on it the resembles Perl6's
35       method signatures.
36
37       Also worth noting is that the error variable ($@) is localised to the
38       try/catch blocks and will not leak outside the scope, or stomp on a
39       previous value of $@.
40
41       The simplest case of a catch block is just
42
43        catch { ... }
44
45       where upon the error is available in the standard $@ variable and no
46       type checking is performed. The exception can instead be accessed via a
47       named lexical variable by providing a simple signature to the catch
48       block as follows:
49
50        catch ($err) { ... }
51
52       Type checking of the exception can be performed by specifing a type
53       constraint or where clauses in the signature as follows:
54
55        catch (TypeFoo $e) { ... }
56        catch (Dict[code => Int, message => Str] $err) { ... }
57
58       As shown in the above example, complex Moose types can be used,
59       including MooseX::Types style of type constraints
60
61       In addition to type checking via Moose type constraints, you can also
62       use where clauses to only match a certain sub-condition on an error.
63       For example, assuming that "HTTPError" is a suitably defined TC:
64
65        catch (HTTPError $e where { $_->code >= 400 && $_->code <= 499 } ) {
66          return "4XX error";
67        }
68        catch (HTTPError $e) {
69          return "other http code";
70        }
71
72       would return "4XX error" in the case of a 404 error, and "other http
73       code" in the case of a 302.
74
75       In the case where multiple catch blocks are present, the first one that
76       matches the type constraints (if any) will executed.
77

BENEFITS

79       return. You can put a return in a try block, and it would do the right
80       thing - namely return a value from the subroutine you are in, instead
81       of just from the eval block.
82
83       Type Checking. This is nothing you couldn't do manually yourself, it
84       does it for you using Moose type constraints.
85

TODO

87       ·   Decide on "finally" semantics w.r.t return values.
88
89       ·   Write some more documentation
90
91       ·   Split out the dependancy on Moose
92

SEE ALSO

94       MooseX::Types, Moose::Util::TypeConstraints, Parse::Method::Signatures.
95

AUTHOR

97       Ash Berlin <ash@cpan.org>
98

THANKS

100       Thanks to Matt S Trout and Florian Ragwitz for work on Devel::Declare
101       and various B::Hooks modules
102
103       Vincent Pit for Scope::Upper that makes the return from block possible.
104
105       Zefram for providing support and XS guidance.
106
107       Xavier Bergade for the impetus to finally fix this module in 5.12.
108

LICENSE

110       Licensed under the same terms as Perl itself.
111
112
113
114perl v5.32.0                      2020-07-28                       TryCatch(3)
Impressum