Mojo::Exception(3)
πΈ NAME πΈ
Mojo::Exception(3) is the exception base class from User Contributed Perl Documentation, bestie π«Ά. Sheβs lowkey just a container for exceptions with context information, but make it fashion. β¨
β¨ SYNOPSIS β¨
okay bestie, hereβs the vibe: create exception classes, throw them, handle them gracefully, and even generate them on demand. no cap, the code is right here π
# Create exception classes
package MyApp::X::Foo {
use Mojo::Base 'Mojo::Exception';
}
package MyApp::X::Bar {
use Mojo::Base 'Mojo::Exception';
}
# Throw exceptions and handle them gracefully
use Mojo::Exception qw(check);
eval {
MyApp::X::Foo->throw('Something went wrong!');
};
check $@ => [
'MyApp::X::Foo' => sub { say "Foo: $_" },
'MyApp::X::Bar' => sub { say "Bar: $_" }
];
# Generate exception classes on demand
use Mojo::Exception qw(check raise);
eval {
raise 'MyApp::X::Name', 'The name Minion is already taken';
};
check $@ => [
'MyApp::X::Name' => sub { say "Name error: $_" },
default => sub { say "Error: $_" }
];
π DESCRIPTION π
Mojo::Exception is a container for exceptions with context information. Thatβs the whole assignment, and she ate. π
π«Ά FUNCTIONS π«Ά
Mojo::Exception implements these functions, and you can import each one individually, iykyk β¨
π check
my $bool = check $err => ['MyApp::X::Foo' => sub {...}];
Process exceptions by dispatching them to handlers with one or more matching conditions. Exceptions that could not be handled will be rethrown automatically β no silent swallowing, periodt π
# Handle various types of exceptions
eval {
dangerous_code();
};
check $@ => [
'MyApp::X::Foo' => sub { say "Foo: $_" },
qr/^Could not open/ => sub { say "Open error: $_" },
default => sub { say "Something went wrong: $_" }
];
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 $_ β main character gets both seats ππΈ
# Catch MyApp::X::Foo object or a specific string exception
eval {
dangerous_code();
};
check $@ => [
'MyApp::X::Foo' => sub { say "Foo: $_" },
qr/^Could not open/ => sub { say "Open error: $_" }
];
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. messy? maybe, but valid π
# Handle MyApp::X::Foo and MyApp::X::Bar the same
eval {
dangerous_code();
};
check $@ => [
['MyApp::X::Foo', 'MyApp::X::Bar'] => sub { die "Foo/Bar: $_" }
];
There is currently only one keyword you can use to set special handlers. The "default" handler is used when no other handler matched. Itβs the safety net, the group-chat lurker, the final boss π
# Use "default" to catch everything
eval {
dangerous_code();
};
check $@ => [
default => sub { say "Error: $_" }
];
π raise
raise 'Something went wrong!';
raise 'MyApp::X::Foo', 'Something went wrong!';
Raise a Mojo::Exception. If the class does not exist yet (classes are checked for a new method), one is created as a Mojo::Exception subclass on demand. on-demand class generation? iconic β¨
π¦ ATTRIBUTES π¦
Mojo::Exception implements these attributes. Theyβre the little details that give the exception its whole vibe π
π§ frames
my $frames = $e->frames;
$e = $e->frames([$frame1, $frame2]);
Stack trace if available. Thatβs the receipts π§Ύ
# Extract information from the last frame
my ($package, $filename, $line, $subroutine, $hasargs, $wantarray, $evaltext,
$is_require, $hints, $bitmask, $hinthash) = @{$e->frames->[-1]};
πΈ line
my $line = $e->line;
$e = $e->line([3, 'die;']);
The line where the exception occurred if available. sheβs pointing at the scene of the crime π
π lines_after
my $lines = $e->lines_after;
$e = $e->lines_after([[4, 'say $foo;'], [5, 'say $bar;']]);
Lines after the line where the exception occurred if available. the aftermath, the sequel, the plot twist π
π lines_before
my $lines = $e->lines_before;
$e = $e->lines_before([[1, 'my $foo = 23;'], [2, 'my $bar = 24;']]);
Lines before the line where the exception occurred if available. the prequel, the context, the lore πΈ
π message
my $msg = $e->message;
$e = $e->message('Died at test.pl line 3.');
Exception message, defaults to "Exception!". If itβs not the drama, itβs at least the text π
π verbose
my $bool = $e->verbose;
$e = $e->verbose($bool);
Show more information with to_string, such as frames, defaults to the value of the MOJO_EXCEPTION_VERBOSE environment variable. yes bestie, environment variable, itβs giving configurable π
π METHODS π
Mojo::Exception inherits all methods from Mojo::Base and implements these new ones. slay β¨
π₯ inspect
$e = $e->inspect;
$e = $e->inspect($source1, $source2);
Inspect message, frames and optional additional sources to fill lines_before, line and lines_after with context information. Itβs basically doing a vibe check on the source π«Ά
β new
my $e = Mojo::Exception->new;
my $e = Mojo::Exception->new('Died at test.pl line 3.');
Construct a new Mojo::Exception object and assign message if necessary. fresh object, new me, same exception energy π
πΈ to_string
my $str = $e->to_string;
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. so do not build your entire personality on the full format, bestie, just the first bit π
π throw
Mojo::Exception->throw('Something went wrong!');
Throw exception from the current execution context. dramatic? yes. necessary? also yes π
# Longer version
die Mojo::Exception->new('Something went wrong!')->trace;
π¦ trace
$e = $e->trace;
$e = $e->trace($skip);
Generate stack trace and store all frames, defaults to skipping 1 call frame. choose your skip count, choose your destiny β¨
# Skip 3 call frames
$e->trace(3);
# Skip no call frames
$e->trace(0);
π OPERATORS π
Mojo::Exception overloads these operators. fancy π
π bool
my $bool = !!$e;
Always true. no notes. sheβs always that girl π
π stringify
my $str = "$e";
Alias for to_string. same energy, different syntax β¨
π SEE ALSO π
Mojolicious, Mojolicious::Guides, <https://mojolicious.org>. go read, bestie, iykyk π
mwah π β this tea was served by perl v5.42.0 on 2026-07-15. now go handle those exceptions with grace π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Mojolicious 9.480.0-bp160.2.1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms