Try-Tiny

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

    some cases during global destruction (Graham Knop, doy/Sub-Name/#9)

0.16  2013-07-10
  - remove accidental Sub::Name test dep

0.15  2013-07-08
  - optionally use Sub::Name to name the try/catch/finally blocks, if available
    (Mark Fowler)

0.14  2013-07-05
  - also throw an exception for catch/finally in scalar context (RT#81070)

0.13  2013-07-04
  - fix tests failing on 5.6.x due to differing DESTROY semantics
  - excise superfluous local($@) call - 7% speedup
  - fix (fsvo) broken URLs (RT#55659)
  - proper exception on erroneous usage of bare catch/finally (RT#81070)
  - proper exception on erroneous use of multiple catch{} blocks
  - clarify exception occuring on unterminated try block (RT#75712)
  - fix the prototypes shown in docs to match code (RT#79590; thanks, Pushtaev
    Vadim)

lib/Try/Tiny.pm  view on Meta::CPAN

C<Try::Tiny::Catch> which allows try to decode correctly what to do
with this code reference.

  catch { ... }

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

For code that captures C<$@> when throwing new errors (i.e.
L<Class::Throwable>), you'll need to do:

  local $@ = $_;

=item finally (&;@)

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

t/basic.t  view on Meta::CPAN

  my ( $code, $desc ) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;

  my ( $ok, $error ) = _eval($code);

  ok($ok, $desc );

  diag "error: $@" unless $ok;
}

sub throws_ok (&$$) {
  my ( $code, $regex, $desc ) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;

  my ( $ok, $error ) = _eval($code);

  if ( $ok ) {
    fail($desc);
  } else {
    like($error || '', $regex, $desc );
  }

t/erroneous_usage.t  view on Meta::CPAN


use Test::More tests => 8;
use Try::Tiny;

sub _eval {
  local $@;
  local $Test::Builder::Level = $Test::Builder::Level + 2;
  return ( scalar(eval { $_[0]->(); 1 }), $@ );
}

sub throws_ok (&$$) {
  my ( $code, $regex, $desc ) = @_;
  local $Test::Builder::Level = $Test::Builder::Level + 1;

  my ( $ok, $error ) = _eval($code);

  if ( $ok ) {
    fail($desc);
  } else {
    like($error || '', $regex, $desc );
  }
}

throws_ok {
  try { 1 }; catch { 2 };
} qr/\QUseless bare catch()/, 'Bare catch() detected';

throws_ok {
  try { 1 }; finally { 2 };
} qr/\QUseless bare finally()/, 'Bare finally() detected';

throws_ok {
  try { 1 }; catch { 2 } finally { 2 };
} qr/\QUseless bare catch()/, 'Bare catch()/finally() detected';

throws_ok {
  try { 1 }; finally { 2 } catch { 2 };
} qr/\QUseless bare finally()/, 'Bare finally()/catch() detected';


throws_ok {
  try { 1 } catch { 2 } catch { 3 } finally { 4 } finally { 5 }
} qr/\QA try() may not be followed by multiple catch() blocks/, 'Multi-catch detected';


throws_ok {
  try { 1 } catch { 2 }
  do { 2 }
} qr/\Qtry() encountered an unexpected argument (2) - perhaps a missing semi-colon before or at/,
  'Unterminated try detected';

sub foo {
  try { 0 }; catch { 2 }
}

throws_ok {
  if (foo()) {
    # ...
  }
} qr/\QUseless bare catch/,
  'Bare catch at the end of a function call';

sub bar {
  try { 0 }; finally { 2 }
}

throws_ok {
  if (bar()) {
    # ...
  }
} qr/\QUseless bare finally/,
  'Bare finally at the end of a function call';

t/finally.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More tests => 30;
use Try::Tiny;

try {
  my $a = 1+1;
} catch {
  fail('Cannot go into catch block because we did not throw an exception')
} finally {
  pass('Moved into finally from try');
};

try {
  die('Die');
} catch {
  ok($_ =~ /Die/, 'Error text as expected');
  pass('Into catch block as we died in try');
} finally {

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.298 second using v1.00-cache-2.02-grep-82fe00e-cpan-3b7f77b76a6c )