Perl Error Handling

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.


Perl Script without Die function

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.

Output:

Print() on closed filehandle $fh at script.pl 
done

Look at the above output, the script keeps on running on encountering error printing 'done'.


Perl Open or Die

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.

Output:

Died at script.pl

In the output, as we use die() function script exit on encountering error. Hence, 'done' is not printed.


Perl Adding Explanation in Die

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.

Output:

Could not open file due to 'sssit/javatpoint/report.txt'at script.pl

Look at the above output, we got an explanation about the error in our script.


Perl Error Reporting using $!

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.

Output:

Could not open file 'sssit/javatpoint/file1.txt' No such file or directory

Perl warn Function

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.

Output:

Can't open file at hw.pl at line 4.
done

Look at the above output, we have printed 'done' to show that execution continues even after printing warning message.

Perl Error Reporting using confess Function

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.

Output:

No such file or directory.
done

Perl eval Function

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 $@; }

Output:

Script is still running!
Illegal division by zero

Look at the above output, script keeps on running as their is no syntax error.

Difference between die() and confess()

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.

Next TopicPerl DBI