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

Consider the following example #! /bin/perl use strict; sub sample { my @arr=(1,2,3,4); return @arr; } my ($a,$b,$c,$d) = &sample; print "$a\n$b\n$c\n$d\n"; In the above code, How can I get the $c without using the arguments such as $a,$b. I don't want to use any array to get the return values.

2 Answers  


Difference between the variables in which chomp function work ?

0 Answers  


What syntax is used for grep() function?

0 Answers  


You want to concatenate strings with perl. How would you do that?

0 Answers  


We all know private variables exist in perl. But do private METHODS exist in perl ? Eg ?

0 Answers   AppLabs,






How to read a directory in perl?

0 Answers  


What are the advantages of c over Perl?

0 Answers  


What is perl shift array function?

0 Answers  


How do I read command-line arguments with Perl?

0 Answers  


How to open and read data files with Perl

0 Answers  


How will you declare a variable in perl?

0 Answers  


Does Perl have reference type?

0 Answers  


Categories