How would you trap error occurred in the perl program/file?

Answer Posted / prabhath kota

You can catch the errros by using eval function.
Keep your code in eval block some thing like shown below.
Eg.,
#################################
eval {
my $a = 0;
my $b = $a/0; #Dividing 0 with 0 is definitely an error
};

if ($@) {
print "\n Error in your code";
}


############################
-> Eval block always ends with a semi-colon. $@ will catch
the errors persent.

-> If any errors are present $@ will be set otherwise $@
will not be set

-> Unfortunately in Perl we don't have Explicit Error
handling techniques like some other languages like java etc
I mean like IOException etc.,

Is This Answer Correct ?    5 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the different string manipulation operators in perl?

529


Explain the arguments for perl interpreter.

523


How do I sort a hash by the hash key?

568


Which functions in perl allows you to include a module file.

496


How we can navigate the xml documents?

535






What exactly is grooving and shortening of the array?

592


How many types of variable in perl?

551


Explain strftime() function in perl?

538


Can inheritance be used in perl? Explain with the help of an example.

505


Explain regular expression in perl?

528


What are the various flags/arguments that can be used while executing a perl program?

457


What is perl I used for?

527


What is use of ‘->’ symbol?

530


explain the various functions/directives in perl that allow you to include/import a module. Also, state the differences between them.

460


How to create a directory in perl?

523