Error within Modules

So far we have seen how Perl handles and informs about the errors to the programmer, but what if there was an error in a module that needs to be informed to the programmer? Such errors will cause difficulty in execution of all those codes which import that particular module.

For example, you make a module let’s say test.pm, and we are doing a wrong calculation, so an error message will be thrown as shown below:

perl




package test;
sub functionName
{
   warn "Error in module!"
}
1;


use test;
functionName();

Output:

Error in module! at test.pm line 6

This error handling technique can be used by the programmers to rectify all the errors in the module. Carp module() can be used to provide methods for reporting errors within modules.

Error Handling in Perl

Error Handling in Perl is the process of taking appropriate action against a program that causes difficulty in execution because of some error in the code or the compiler. Processes are prone to errors. For example, if opening a file that does not exist raises an error, or accessing a variable that has not been declared raises an error.
The program will halt if an error occurs, and thus using error handling we can take appropriate action instead of halting a program entirely. Error Handling is a major requirement for any language that is prone to errors.

Errors can be classified by the time at which they occur:

  1. Compile Time Errors
  2. Run Time Errors

Compile Time Error: It is an error such as syntax error or missing file reference that prevents the program from successfully compiling.

Run Time Error : It is an error that occurs when the program is running and these errors are usually logical errors that produce the incorrect output.

Similar Reads

Error Handling in Perl

Perl provides two builtin functions to generate fatal exceptions and warnings, that are:...

Error within Modules

...

The Carp Module

...

Contact Us