Tomas Pokorny > Syntax-Feature-Try-0.002 > Syntax::Feature::Try

Download:
Syntax-Feature-Try-0.002.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  1
View/Report Bugs
Module Version: 0.002   Source   Latest Release: Syntax-Feature-Try-1.000

NAME ^

Syntax::Feature::Try - try/catch/finally statement for exception handling

SYNOPSIS ^

    use syntax 'try';

    try {
        # run this code and handle errors
    }
    catch (My::Class::Err $e) {
        # handle exception based on class "My::Class::Err"
    }
    catch ($e) {
        # handle other exceptions
    }
    finally {
        # cleanup block
    }

DESCRIPTION ^

This module implements syntax for try/catch/finally statement with behaviour similar to other programming languages (like Java, Python, etc.).

It uses perl ( >= 5.14 ) experimental parser/lexer API.

SYNTAX ^

initiliazation

To initialize this syntax feature call:

    use syntax 'try';

try

The try block is executed. If it throws an error, then first catch block (in order) that can handle thrown error will be executed. Other catch blocks will be skipped.

If none of catch blocks can handle the error, it is thrown out of whole statement. It try block doe not throw an error, all catch blocks are skipped.

catch error class

    catch (My::Error $err) { ... }

This catch block can handle error that is instance of class My::Error or any of it's subclasses.

Caught error is accessible inside catch block via declared local variable $err.

catch all errors

To catch all errors use syntax:

    catch ($e) { ... }

Caught error is acessible inside catch block via declared local variable $e.

rethrow error

To rethrow caught error simple call "die $err". For example (log any Connection::Error):

    try { ... }
    catch (Connection::Error $err) {
        log_error($err);
        die $err;
    }

finally

The "finally block" is executed at the end of statement. It is always executed (even if try or catch block throw an error).

    my $fh;
    try {
        $fh = IO::File->new("/etc/hosts");
        ...
    }
    finally {
        $fh->close;
    }

Exception::Class ^

This module is compatible with Exception::Class

    use Exception::Class (
        'My::Test::Error'
    );
    use syntax 'try';

    try {
        ...
        My::Test::Error->throw('invalid password');
    }
    catch (My::Test::Error $err) {
        # handle error here
    }

CAVEATS ^

@_

@_ is not accessible inside try/catch/finally blocks, because these blocks are internally called in different context.

return, wantarray

return and wantarray is not working inside try/catch/finally blocks, because these blocks are internally called in different context.

next, last, redo

next, last and redo is not working inside try/catch/finally blocks, because these blocks are internally called in different context.

TODO ^

return, wantarray, ...

BUGS ^

SEE ALSO ^

syntax - Active syntax extensions

Exception::Class - A module that allows you to declare real exception classes in Perl

Other similar packages

TryCatch - first class try catch semantics for Perl

Try - nicer exception handling syntax

AUTHOR ^

Tomas Pokorny <tnt at cpan dot org>

COPYRIGHT AND LICENCE ^

Copyright 2013 - Tomas Pokorny.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: