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
Which functions in Perl allows you to include a module file or a module and what is the difference between them?
How to connect to SQL server through Perl?
Assuming both a local($var) and a my($var) exist, what's the difference between ${var} and ${"var"}?
We all know private variables exist in perl. But do private METHODS exist in perl ? Eg ?
Define operators used in perl?
What is the closure in PERL?
How do find the length of an array?
What is the easiest way to download the contents of a URL with Perl?
How can you replace the characters from a string and save the number of replacements?
What are the logical operators used for small scale operations?
What are the various perl data types based on the context?
Explain the difference between die and exit in perl?
You want to download the contents of a url with perl. How would you do that?
There are two types of eval statements i.e. Eval expr and eval block. Explain them.
How and what are closures implemented in perl?