Perl Error Handling

Error basically two types, One is some logical mistakes and misspell or syntax. And second type of error are occurrence during computation of instruction. Both types of error are fetch by perl interpreter. First is basic error which are based on programmer wrong syntax and misspelling. Interpreter are never run this type of error under {use strict} statement.

Second error are very dangerous, Because this are based on a other resources if resources are not found then it will produce wrong result or can be produced unwanted warning. For example.

use strict;  
use strict;  
use warnings; 

print "\nOperation Start";
open(my $fh, '<:encoding(UTF-8)', 'test/info.txt')
  or die "Could not open file  $!";
print "\nFile Data\n";
while (my $row = <$fh>) {
    chomp $row;
    print "$row\n";
print "done\n"
Operation Start
File Data
I am Porgrammer

In this example info.txt file are exist inside a test directory. So program are execute without any error or waring. But in other case when file are not exit in this location. Then program are terminated by using of die function.

Suppose in this example not use die method then perl interpreter always produce a warning (when file open or not open by open method). So there are possible file are not exist at given location or can be other reason file are not allow to read and write access.

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment

© 2021,, All rights reserved