try { ... }; # mind the ';' !! if($@) { # signals something went wrong if(try {...}) { # block ended normally my $x = try { read_temperature() }; my @x = try { read_lines_from_file() }; try { ... } # no comma!! mode => 'DEBUG', accept => 'ERROR-'; try sub { ... }, # with comma mode => 'DEBUG', accept => 'ALL'; try \&myhandler, accept => 'ERROR-'; try { ... } hide => 'TRACE'; print ref $@; # Log::Report::Dispatcher::Try $@->reportFatal; # re-dispatch result of try block $@->reportAll; # ... also warnings etc if($@) {...} # if errors if($@->failed) { # same # } if($@->success) { # no errors # } try { # something causes an error report, which is caught failure 'no network'; }; $@->reportFatal(to => 'syslog'); # overrule destination print $@->exceptions; # no re-cast, just print
See SYNOPSIS in Log::Report::Dispatcher
The try works like Perl's build-in eval()
, but implements
real exception handling which Perl core lacks.
The Log::Report::try() function creates this ::Try
dispatcher
object with name 'try'. After the try()
is over, you can find
the object in $@
. The $@
as ::Try
object behaves exactly
as the $@
produced by eval
, but has many added features.
The try()
function catches fatal errors happening inside the BLOCK
(CODE reference which is just following the function name) into the
::Try
object $@
. The errors are not automatically progressed to
active dispatchers. However, non-fatal exceptions (like info or notice)
are also collected (unless not accepted, see new(accept), but also
immediately passed to the active dispatchers (unless the reason is hidden,
see new(hide))
After the try()
has run, you can introspect the collected exceptions.
Typically, you use wasFatal() to get the exception which terminated
the run of the BLOCK.
See DESCRIPTION in Log::Report::Dispatcher
See Available back-ends in Log::Report::Dispatcher
See Addition information in Log::Report::Dispatcher
See Filters in Log::Report::Dispatcher