On execution of a program, errors also run along with the program. If these errors will not be handled properly, then the program may not run smoothly.
Error handling let us handle these errors by taking appropriate action and run program smoothly. Various types of errors can be handled by error handling.
You need to catch the error and run your program smoothly by rectifying the error. Perl provides a number of ways to do so which are illustrated below.
The die() function gives you a proper error message. It immediately terminates the script on encountering an error. If you won't use die() function in your script, your script will keep on running.
use strict; use warnings; open(my $fh, '>', 'sssit/rookienerd/file1.txt'); print $fh "Example to show Error Handling\n"; close $fh; print "done\n";
Look at the above output, the script keeps on running on encountering error printing 'done'.
The open() function will only open your file as usual. The die() function throws an exception and just exit the script.
In 'open or die' function, on left side we have open() function. On right side we have die() function.
If open() function returns true, then script goes on next line and die() function doesn't execute.
If open() function returns false, then script goes on die() function which throws an exception and exit the script.
Here, in this example we have given the wrong path of the file due to which die() function will execute and exit the script.
use strict; use warnings; open(my $fh, '>', 'sssit/rookienerd/file1.txt') or die; print $fh "Example to show Error Handling\n"; close $fh; print "done\n";
In the output, as we use die() function script exit on encountering error. Hence, 'done' is not printed.
If you want to add some explanation about the error, you can add it in die() function. If your script dies, this explanation will print as the error message.
use strict; use warnings; open(my $fh, '>', 'sssit/rookienerd/report.txt') or die "Could not open file due to 'sssit/rookienerd/report.txt'"; close $fh; print "done\n";
Look at the above output, we got an explanation about the error in our script.
The $! Variable is a built in variable in Perl language. By adding explanation in die() function, we know the error message but we still don't know the reason behind it. To know the exact reason of error use $! variable. It will print the message told by operating system about the file.
use strict; use warnings; my $filename = 'sssit/rookienerd/file1.txt'; open(my $fh, '>', $filename) or die "Could not open file '$filename' $!"; close $fh; print "done\n";
A warn function gives a warning message but does not exit the script. The script will keep on running. Hence, it is useful when you only want to print the warning message and proceed for the rest of the program.
use strict; use warnings; my $filename = 'sssit/rookienerd/file1.txt'; open(my $fh, '>', $filename) or warn "Can't open file"; print "done\n";
Look at the above output, we have printed 'done' to show that execution continues even after printing warning message.
The modern approach for error handling is to use Carp standard library. The confess() function is used within Carp library. We have passed $! as its argument.
use strict; use warnings; use Carp; my $filename = 'sssit/rookienerd/file1.txt'; open(my $fh, '>', $filename) or confess($!); print "done\n";
An eval() function is a built-in function in Perl which is used to detect normal fatal error. The eval() function is supplied with a code block instead of passing into string.
If there are syntax errors, eval block will fail. But if a runtime error occurs, the script carries on running.
In the following program,there is no syntax error.
use strict; use warnings; my $result = eval { my $x = 10; my $y = 0; my $result2 = $x/$y; print "$result2"; }; print "Script is still running!\n"; unless($result) { print $@; }
Look at the above output, script keeps on running as their is no syntax error.
The die() function is used when script short comprising of ten lines. The die() function can also be used without $!.
The confess() function is used inside carp package. For a larger script, it is better to use confess function.