How can I check what kind of exception caused the script or eval block to terminate?
I need to know the type of error, and where the exception occurred.
How can I check what kind of exception caused the script or eval block
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The Perl way
Idiomatic Perl is that we are either ignoring all errors or capturing them for logging or forwarding elsewhere:
or:
or (using Try::Tiny – see that page for reasons why you might want to use it over Perl’s built-in exception handling):
If you want to check the type of exception, use regexes:
The line and file is also in that string too.
This is pretty nasty though, because you have to know the exact string. If you really want good error handling, use an exception module from CPAN.
Exception::Class
$@ doesn’t have to be a string, it can be an object. Exception::Class lets you declare and throw exception objects Java-style. You can pass arbitrary information (filename, etc.) with the error when you throw it and get that information out using object methods rather than regex parsing – including the file and line number of the exception.
If you’re using a third party module that does not use Error::Exception, consider
This will transform all errors into Exception::Class objects.
Error::Exception sets up proper stringification for Exception::Class objects.