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

How to create a package?

517


Write an example explaining the use of symbol tables.

557


Which functions in perl allows you to include a module file.

489


What do you mean by context of a subroutine?

552


Which functions in perl allows you to include a module file. State their differences.

519






What are perl array functions?

521


Explain join function in perl?

528


What are perl strings?

498


How to read a single line from a file in perl?

530


What are the purpose of close(), getc() and read() functions?

479


What $! In perl?

519


What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), $ap ]; }' ?

538


How do I sort a hash by the hash value?

547


What are stdin, stdout and stderr?

505


What is cpan in perl?

542