Log::Report::Dispatcher::Try(3pm) | User Contributed Perl Documentation | Log::Report::Dispatcher::Try(3pm) |
Log::Report::Dispatcher::Try - capture all reports as exceptions
Log::Report::Dispatcher::Try is a Log::Report::Dispatcher
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
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.
Extends "DESCRIPTION" in Log::Report::Dispatcher.
Extends "METHODS" in Log::Report::Dispatcher.
Extends "Constructors" in Log::Report::Dispatcher.
-Option --Defined in --Default accept Log::Report::Dispatcher depend on mode charset Log::Report::Dispatcher <undef> died undef exceptions [] format_reason Log::Report::Dispatcher 'LOWERCASE' hide 'NONE' locale Log::Report::Dispatcher <system locale> mode Log::Report::Dispatcher 'NORMAL' on_die 'ERROR'
Extends "Accessors" in Log::Report::Dispatcher.
This option gives the opportunity to stop, for instance, trace messages. Those messages are still collected inside the try object (unless excluded by new(accept)), so may get passed-on later via reportAll() if you like.
Be warned: Using this method will reset the whole 'hide' configuration: it's a set not an add.
example: change the setting of the running block
my $parent_try = dispatcher 'active-try'; $parent_try->hide('ALL');
Extends "Logging" in Log::Report::Dispatcher.
The $message is either a STRING or a Log::Report::Message.
The message is not very informative for the good cause: we do not want people to simply print the $@, but wish for a re-cast of the message using reportAll() or reportFatal().
-Option--Default class undef
Extends "DETAILS" in Log::Report::Dispatcher.
This module is part of Log-Report distribution version 1.36, built on October 27, 2023. Website: http://perl.overmeer.net/CPAN/
Copyrights 2007-2023 by [Mark Overmeer <markov@cpan.org>]. For other contributors see ChangeLog.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See http://dev.perl.org/licenses/
2023-10-29 | perl v5.36.0 |