This tutorial explains about how to handle errors and exceptions in Perl Language.
Prior to Perl version 5.34, To hand handle exceptions, Third party libraries such as Try::Tiny
used .
Here is an example before
use Try::Tiny;
try {
die "foo";
} catch {
warn "error thrown: $_";
};
Perl version 5.34 introduced try
,catch
, finally syntax features
These are experimental feature, enabled by using below command.
use feature 'try'
Syntax:
try {
// code
}
catch ($e) {
warn "catch block; $e";
}
finally {
print "finally block\n";
}
finally block is an optional.
if finally included, It executes the code inside it after code executed in try and catch blocks.
Notes:
- finally block does not allow to use return, goto or loop controls.