There are various ways of doing that, but they're not built into Perl directly.
The simplest one is to die and eval {} to catch it.
eval {
die "in a fire";
};
if ($@) {
print "something went wrong";
}
If you want try, again there are various options. The most common one is Try::Tiny.
use Try::Tiny;
try {
die;
} catch {
print $_;
};
If you want to be cutting edge, there's research done by Paul Evans to get an actual keyword try into the Perl code. He's released a prototype of this as Syntax::Keyword::Try and has given various talks about it recently, including at Fosdem in 2021.
Now for the actual exceptions, there are, as is typical of Perl, several ways of doing that.
die can take objects as its argument rather than just strings, so you can pretty much emulate the behaviour of other languages.
Throwable is probably what I would go for today. You can easily create lots of these classes on the fly with Throwable::Factory.
use Throwable::Factory
InvalidArgumentException => [qw( $id )];
sub foo {
my $group_id = shift;
unless ($group_id) {
InvalidArgumentException->throw('Current group not found', id => $group_id);
}
}
And later on to catch that, you can do:
use Try::Tiny;
try {
foo(0);
} catch {
warn $_;
};