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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / shah faisal

#Ist trapping the error in a block
eval {
# perl code goes here
};

if ($@) { # $@ is a special variable containing the error
if any else blank
print "Print your own error If u want";
}

# 2nd
# use if or unless for a loop ;ike below
if(chk some condition)
{
die"$!\n"; # if the condition success
}
system generated error is printed and the scirpt is exited
}

Is This Answer Correct ?    0 Yes 0 No

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

Answer / perly_whirly

Put your code inside of an eval. If an error occurs, the $@ variable will contain
the error. You can also raise an error by using die inside of an eval.

Is This Answer Correct ?    1 Yes 3 No

Post New Answer

More CGI Perl Interview Questions

How to convert arrays into a string in perl?

0 Answers  


Why does Perl not have overloaded functions?

0 Answers  


Mention what is cpan?

0 Answers  


Explain the meaning of perl one-liner?

0 Answers  


How to make the following assignment, as arrayreference assignment ? my $arr_ref='[1,2,3,4,4,'elem']';

2 Answers  






What does file test operators do in perl?

0 Answers  


I have one question regarding to eval function. I know eval function is use for error checking but I am not able to understand below line. eval \'exec perl -S $0 ${1+\"$@\"}\' if 0; $0 for script name $@ set if error occur

0 Answers  


What are numeric operators in perl?

0 Answers  


When would `local $_' in a function ruin your day?

0 Answers  


Differentiate use and require?

0 Answers  


What does length(%HASH) produce if you have thirty-seven random keys in a newly created hash?

0 Answers  


Why do you use only Perl when there a lot of more languages available in market like C, Java?

1 Answers  


Categories