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
How to create a package?
Write an example explaining the use of symbol tables.
Which functions in perl allows you to include a module file.
What do you mean by context of a subroutine?
Which functions in perl allows you to include a module file. State their differences.
What are perl array functions?
Explain join function in perl?
What are perl strings?
How to read a single line from a file in perl?
What are the purpose of close(), getc() and read() functions?
What $! In perl?
What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), $ap ]; }' ?
How do I sort a hash by the hash value?
What are stdin, stdout and stderr?
What is cpan in perl?