Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl try catch for user defined exceptions

Tags:

perl

I want to know if perl has some try catch mechanism similar to python where i can raise user defined exceptions and handle accordingly.

PythonCode:

   try:
       number = 6
       i_num = 3
       if i_num < number:
           raise ValueTooSmallError
       elif i_num > number:
           raise ValueTooLargeError
       break
   except ValueTooSmallError:
       print("This value is too small, try again!")
       print()
   except ValueTooLargeError:
       print("This value is too large, try again!")
       print()

I know perl has try catch mechnism like below:

sub method_one {
    try {
        if ("number" eq "one") {
            die("one");
        } else {
            die("two");
        }
    } catch {
        if ($@ eq "one") {
            print "Failed at one";
        }
    }
}

OR

eval {
    open(FILE, $file) || 
      die MyFileException->new("Unable to open file - $file");
  };

  if ($@) {
    # now $@ contains the exception object of type MyFileException
    print $@->getErrorMessage();  
    # where getErrorMessage() is a method in MyFileException class
  }

I am concentrating more on the if checks on the catch. Is there a way i can avoid the checks for the different kind of errors i catch.

like image 627
Chetan Avatar asked Feb 26 '18 07:02

Chetan


People also ask

How do I catch an exception in Perl?

Catch the exception using evalIf we wrap a piece of code in an eval block, the eval will capture any exception that might happen within that block, and it will assign the error message to the $@ variable of Perl. The simple way to do this looks like this: (please note, there is a ; after the eval block.)

Is there a try catch in Perl?

The try and catch block of code is used for error handling in Perl, similar to other languages such as Java and Python. The try and catch block tries the code meant to be executed in the try block, and if an error occurs, the program does not crash. Instead, the error is caught and printed by the catch block.

Does Perl have exceptions?

Perl doesn't have exceptions.


2 Answers

The closest solution is probably the straight-forward failures object and TryCatch for the type checking.

use failures qw(
    Example::Value_too_small
    Example::Value_too_large
);
use TryCatch;

try {
    my $number = 6;
    my $i_num = 3;
    if ($i_num < $number) {
        failure::Example::Value_too_small->throw({
            msg => '%d is too small, try again!',
            payload => $i_num,
        });
    } elsif ($i_num > $number) {
        failure::Example::Value_too_large->throw({
            msg => '%d is too large, try again!',
            payload => $i_num,
        });
    }
} catch (failure::Example::Value_too_small $e) {
    say sprintf $e->msg, $e->payload;
} catch (failure::Example::Value_too_large $e) {
    say sprintf $e->msg, $e->payload;
} finally {
    ...
}

You can upgrade from here to custom::failures, Throwable, Exception::Class.

like image 181
daxim Avatar answered Sep 29 '22 20:09

daxim


Like @daxim pointed out, there is TryCatch, but I am writing this for all those who would see this and to alert them that unfortunately TryCatch is now broken since the version 0.006020 of Devel::Declare on which TryCatch relies.

In replacement, there is Nice::Try which is quite unique and provides all the features like in other programming languages.

It supports exception variable assignment, exception class like the OP requested, clean-up with finally block, embedded try-catch blocks.

Full disclosure: I have developed Nice::Try when TryCatch got broken.

like image 44
Jacques Avatar answered Sep 29 '22 21:09

Jacques