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

What are perl strings?

0 Answers  


Explain split function in perl?

0 Answers  


What does this symbol mean '->'?

0 Answers  


write a Perl script to find a particular word in a paragraph???

1 Answers  


what are prefix dereferencer and list them out?

0 Answers  


How to print escaping characters inside a string in perl?

0 Answers  


What value is returned by a lone `return;’ statement?

0 Answers  


what is the function that is used to identify how many characters are there in a string?

0 Answers  


What is the use of command “use strict”?

0 Answers  


There are two types of eval statements i.e. Eval expr and eval block. Explain them.

0 Answers  


How can memory be managed in Perl?

0 Answers  


Comment on the scope of variables in perl.

0 Answers  


Categories