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 benefits of perl in using it as a web-based application?

496


How to get help for perl?

591


How to prevent file truncation in perl?

503


List all the features of perl programming?

514


What is a chop() function in perl?

579






What are the logical operators used for small scale operations?

548


How do you find the length of an array?

555


How to create a package?

527


Differentiate between arrays and list in perl.

538


What are the various uses of perl?

531


What is the use of command “use strict”?

567


Write an example explaining the use of symbol tables.

564


Explain use of ‘my’ keyword in perl?

576


What does init 5 and init 0 do?

527


Differentiate use and require?

531