diff --git a/lib/Exceptions.pm b/lib/Exceptions.pm index f60818e..039315c 100644 --- a/lib/Exceptions.pm +++ b/lib/Exceptions.pm @@ -95,7 +95,8 @@ die in an C block. This sounds simple enough, but there are some gotchas that lead many developers to do this incorrectly. The correct way to handle an exception requires that you understand how to -preserve the global C<$@> variable. Please see L for a +preserve the global C<$@> variable and that its value cannot be relied upon to +determine whether an exception occurred. Please see L for a great explanation of this problem. Let's look at our previous simple application with error handling using C. @@ -106,32 +107,30 @@ Let's look at our previous simple application with error handling using C. # 1 my $value; - my $error = do { # catch block + my $error; + { # catch block local $@; - eval { $value = increment(0) }; # try - $@; - }; - print "0 plus 1 = ", ($error ? "error": $value), "\n"; + $error = $@ || 'Error' unless eval { $value = increment(0); 1 }; # try + } + print "0 plus 1 = ", (defined $error ? "error": $value), "\n"; # error $value = undef; $error = undef; - $error = do { # catch block + { # catch block local $@; - eval { $value = increment('zero') }; # try - $@; - }; - print "zero plus 1 = ", ($error ? "error": $value), "\n"; + $error = $@ || 'Error' unless eval { $value = increment('zero'); 1 }; # try + } + print "zero plus 1 = ", (defined $error ? "error": $value), "\n"; # 1 $value = undef; $error = undef; - $error = do { # catch block + { # catch block local $@; - eval { $value = increment(0) }; # try - $@; - }; - print "0 plus 1 = ", ($error ? "error": $value), "\n"; + $error = $@ || 'Error' unless eval { $value = increment(0); 1 }; # try + } + print "0 plus 1 = ", (defined $error ? "error": $value), "\n"; sub increment { my $int = shift;