How would you trap error occurred in the perl program/file?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
List the operator used in Perl?
What are the options that can be used to avoid logic errors in perl?
What is the difference between localtime() and gmtime() functions?
What is perl dbi?
If EXPR is an arbitrary expression, what is the difference between $Foo::{EXPR} and *{"Foo::".EXPR}?
How do I send e-mail from a Perl/CGI program on a Unix system?
Perl uses single or double quotes to surround a zero or more characters. Are the single(' ') or double quotes (" ") identical?
How we can navigate the xml documents?
List the data types that Perl can handle?
Does Perl have objects? If yes, then does it force you to use objects? If no, then why?
Explain subroutine in perl?
How to access parameters passed to a subroutine in perl?