I want to know if perl has a try catch mechanism similar to python where I can raise custom exceptions and handle them 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 is trying to catch mechnism, as shown 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 ($@) {
print $@->getErrorMessage();
}
I focus more on if checks on catch. Is there a way to avoid checking for various errors that I catch.
source
share