Web Design San Jose California
ErrorPro MS WINDOWS  | UNIX /LINUX   | ORACLE  |  PERL  |  PYTHON  |  MAC OS »

 

Perl Exception Handling

Exception handling mechanism, the eval {} block.

Implemented by wrapping the code that needs to be executed around an eval block and the $@ variable is checked to see if an exception occurred.
syntax is:

eval {
....
};
if ($@) {
errorHandler($@);
}

Within the eval block, in case a syntax error or runtime error, or a die statement is executed, then an undefined value is returned by eval, and $@ is set to the error message.
No error, then $@ is null string.

Since the error message store in $@ is a simple scalar, checking the type of error that has occurred is error prone $@ does not tell where the exception occurred.

Starting from Perl 5.005, Exception Handling can be done:

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
}

MyFileException exception class can be built with as much functionality as desired.
You can get the calling context by using caller() in constructer of the exception class ( in general MyFileException::new()).




SOURCE: Perl Documentation
ONLINE RESOURCES:
Official Perl website
Download Perl
Open Directory Project
The Perl Foundation
Perl Blogs
Perl Forum

   

Travel Photo Album

Web Hosting  Promotion

Copyright 2008-2009 © ErrorPRO - Free Web Directory - Software Error Codes, Messages, Descriptions, Causes and Recommended Actions.

Get domain of your choice NOW