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 features of perl language?

667


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

552


How can you define “my” variables scope in Perl and how it is different from “local” variable scope?

706


Explain perl. What are the advantages of programming in perl?

664


Explain goto label?

593






How do you set environment variables in perl?

625


What does `$result = f() .. g()' really return?

649


What is an interpolation in perl?

651


How does a “grep” function perform?

672


Write an example explaining the use of symbol tables.

629


What does undef function in perl?

611


What can be done for efficient parameter passing in perl? Explain.

661


Explain what is STDIN, STDOUT and STDERR?

622


Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?

638


What does a die() function do in perl?

643