I have a try catch block in perl
try {
    //statement 1
    //statement 2
};
catch Error with
{
    print "Error\n";
}
When I run the perl program I get the following error
Can't Call method "try" without a package or object reference at...
Perl does not provide try or catch keywords.  To trap "exceptions" thrown by die, you can set a $SIG{__DIE__} handler or use eval.  Block form is preferred over string form, as parsing happens once at compile time.
eval {
    // statement 1
    // statement 2
}
if ($@) {
    warn "caught error: $@";
}
There are various modules that provide more traditional try-like functionality, such as Try::Tiny.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With