TryCatch-1.003002

NAME

DESCRIPTION

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

SYNOPSIS

SYNTAX

This module aims to give first class exception handling to perl via 'try' and 'catch' keywords. The basic syntax this module provides is try { # block } followed by zero or more catch blocks. Each catch block has an optional type constraint on it the resembles Perl6's method signatures.

Also worth noting is that the error variable ($@) is localised to the try/catch blocks and will not leak outside the scope, or stomp on a previous value of $@.

The simplest case of a catch block is just

catch { ... }

where upon the error is available in the standard $@ variable and no type checking is performed. The exception can instead be accessed via a named lexical variable by providing a simple signature to the catch block as follows:

catch ($err) { ... }

Type checking of the exception can be performed by specifing a type constraint or where clauses in the signature as follows:

As shown in the above example, complex Moose types can be used, including MooseX::Types style of type constraints

In addition to type checking via Moose type constraints, you can also use where clauses to only match a certain sub-condition on an error. For example, assuming that HTTPError is a suitably defined TC: