.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Mojo::Exception 3pm" .TH Mojo::Exception 3pm 2024-05-15 "perl v5.38.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH NAME Mojo::Exception \- Exception base class .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 7 \& # Create exception classes \& package MyApp::X::Foo { \& use Mojo::Base \*(AqMojo::Exception\*(Aq; \& } \& package MyApp::X::Bar { \& use Mojo::Base \*(AqMojo::Exception\*(Aq; \& } \& \& # Throw exceptions and handle them gracefully \& use Mojo::Exception qw(check); \& eval { \& MyApp::X::Foo\->throw(\*(AqSomething went wrong!\*(Aq); \& }; \& check $@ => [ \& \*(AqMyApp::X::Foo\*(Aq => sub { say "Foo: $_" }, \& \*(AqMyApp::X::Bar\*(Aq => sub { say "Bar: $_" } \& ]; \& \& # Generate exception classes on demand \& use Mojo::Exception qw(check raise); \& eval { \& raise \*(AqMyApp::X::Name\*(Aq, \*(AqThe name Minion is already taken\*(Aq; \& }; \& check $@ => [ \& \*(AqMyApp::X::Name\*(Aq => sub { say "Name error: $_" }, \& default => sub { say "Error: $_" } \& ]; .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Mojo::Exception is a container for exceptions with context information. .SH FUNCTIONS .IX Header "FUNCTIONS" Mojo::Exception implements the following functions, which can be imported individually. .SS check .IX Subsection "check" .Vb 1 \& my $bool = check $err => [\*(AqMyApp::X::Foo\*(Aq => sub {...}]; .Ve .PP Process exceptions by dispatching them to handlers with one or more matching conditions. Exceptions that could not be handled will be rethrown automatically. Note that this function is \fBEXPERIMENTAL\fR and might change without warning! .PP .Vb 9 \& # Handle various types of exceptions \& eval { \& dangerous_code(); \& }; \& check $@ => [ \& \*(AqMyApp::X::Foo\*(Aq => sub { say "Foo: $_" }, \& qr/^Could not open/ => sub { say "Open error: $_" }, \& default => sub { say "Something went wrong: $_" } \& ]; .Ve .PP Matching conditions can be class names for ISA checks on exception objects, or regular expressions to match string exceptions and stringified exception objects. The matching exception will be the first argument passed to the callback, and is also available as \f(CW$_\fR. .PP .Vb 8 \& # Catch MyApp::X::Foo object or a specific string exception \& eval { \& dangerous_code(); \& }; \& check $@ => [ \& \*(AqMyApp::X::Foo\*(Aq => sub { say "Foo: $_" }, \& qr/^Could not open/ => sub { say "Open error: $_" } \& ]; .Ve .PP An array reference can be used to share the same handler with multiple conditions, of which only one needs to match. And since exception handlers are just callbacks, they can also throw their own exceptions. .PP .Vb 7 \& # Handle MyApp::X::Foo and MyApp::X::Bar the same \& eval { \& dangerous_code(); \& }; \& check $@ => [ \& [\*(AqMyApp::X::Foo\*(Aq, \*(AqMyApp::X::Bar\*(Aq] => sub { die "Foo/Bar: $_" } \& ]; .Ve .PP There is currently only one keywords you can use to set special handlers. The \f(CW\*(C`default\*(C'\fR handler is used when no other handler matched. .PP .Vb 7 \& # Use "default" to catch everything \& eval { \& dangerous_code(); \& }; \& check $@ => [ \& default => sub { say "Error: $_" } \& ]; .Ve .SS raise .IX Subsection "raise" .Vb 2 \& raise \*(AqSomething went wrong!\*(Aq; \& raise \*(AqMyApp::X::Foo\*(Aq, \*(AqSomething went wrong!\*(Aq; .Ve .PP Raise a Mojo::Exception, if the class does not exist yet (classes are checked for a \f(CW\*(C`new\*(C'\fR method), one is created as a Mojo::Exception subclass on demand. Note that this function is \fBEXPERIMENTAL\fR and might change without warning! .SH ATTRIBUTES .IX Header "ATTRIBUTES" Mojo::Exception implements the following attributes. .SS frames .IX Subsection "frames" .Vb 2 \& my $frames = $e\->frames; \& $e = $e\->frames([$frame1, $frame2]); .Ve .PP Stack trace if available. .PP .Vb 3 \& # Extract information from the last frame \& my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext, \& $is_require, $hints, $bitmask, $hinthash) = @{$e\->frames\->[\-1]}; .Ve .SS line .IX Subsection "line" .Vb 2 \& my $line = $e\->line; \& $e = $e\->line([3, \*(Aqdie;\*(Aq]); .Ve .PP The line where the exception occurred if available. .SS lines_after .IX Subsection "lines_after" .Vb 2 \& my $lines = $e\->lines_after; \& $e = $e\->lines_after([[4, \*(Aqsay $foo;\*(Aq], [5, \*(Aqsay $bar;\*(Aq]]); .Ve .PP Lines after the line where the exception occurred if available. .SS lines_before .IX Subsection "lines_before" .Vb 2 \& my $lines = $e\->lines_before; \& $e = $e\->lines_before([[1, \*(Aqmy $foo = 23;\*(Aq], [2, \*(Aqmy $bar = 24;\*(Aq]]); .Ve .PP Lines before the line where the exception occurred if available. .SS message .IX Subsection "message" .Vb 2 \& my $msg = $e\->message; \& $e = $e\->message(\*(AqDied at test.pl line 3.\*(Aq); .Ve .PP Exception message, defaults to \f(CW\*(C`Exception!\*(C'\fR. .SS verbose .IX Subsection "verbose" .Vb 2 \& my $bool = $e\->verbose; \& $e = $e\->verbose($bool); .Ve .PP Show more information with "to_string", such as "frames", defaults to the value of the \&\f(CW\*(C`MOJO_EXCEPTION_VERBOSE\*(C'\fR environment variable. .SH METHODS .IX Header "METHODS" Mojo::Exception inherits all methods from Mojo::Base and implements the following new ones. .SS inspect .IX Subsection "inspect" .Vb 2 \& $e = $e\->inspect; \& $e = $e\->inspect($source1, $source2); .Ve .PP Inspect "message", "frames" and optional additional sources to fill "lines_before", "line" and "lines_after" with context information. .SS new .IX Subsection "new" .Vb 2 \& my $e = Mojo::Exception\->new; \& my $e = Mojo::Exception\->new(\*(AqDied at test.pl line 3.\*(Aq); .Ve .PP Construct a new Mojo::Exception object and assign "message" if necessary. .SS to_string .IX Subsection "to_string" .Vb 1 \& my $str = $e\->to_string; .Ve .PP Render exception. Note that the output format may change as more features are added, only the error message at the beginning is guaranteed not to be modified to allow regex matching. .SS throw .IX Subsection "throw" .Vb 1 \& Mojo::Exception\->throw(\*(AqSomething went wrong!\*(Aq); .Ve .PP Throw exception from the current execution context. .PP .Vb 2 \& # Longer version \& die Mojo::Exception\->new(\*(AqSomething went wrong!\*(Aq)\->trace; .Ve .SS trace .IX Subsection "trace" .Vb 2 \& $e = $e\->trace; \& $e = $e\->trace($skip); .Ve .PP Generate stack trace and store all "frames", defaults to skipping \f(CW1\fR call frame. .PP .Vb 2 \& # Skip 3 call frames \& $e\->trace(3); \& \& # Skip no call frames \& $e\->trace(0); .Ve .SH OPERATORS .IX Header "OPERATORS" Mojo::Exception overloads the following operators. .SS bool .IX Subsection "bool" .Vb 1 \& my $bool = !!$e; .Ve .PP Always true. .SS stringify .IX Subsection "stringify" .Vb 1 \& my $str = "$e"; .Ve .PP Alias for "to_string". .SH "SEE ALSO" .IX Header "SEE ALSO" Mojolicious, Mojolicious::Guides, .