Error Handling

                                                                                                                         

 

Error Handling

  • The execution and the errors always go together. There may be any reasons of getting error during file handling. And if you did not handle this situation properly then your program is considered to be of bad quality.
  • You can identify and trap an error in a number of different and easy ways in PERL .

Warn Function :

  • The warn function just raises a warning, a message is printed to STDERR, but no further action is taken.

Example: Suppose I want to open a file and file doesn’t exist.

open DATA, “+>file.txt” or warn “Couldn’t open file file.txt, $!”;

The $! Variable:

— When an error is recorded by Perl, it stores the error number in a special variable called $!.

— When examined numerically, $! shows a number, but when examined as a string it shows an error message from the operating system.

Die Function:

  • The die function works just like warn, except that it also calls exit. Within a normal script, this function has the effect of immediately terminating execution.

Syntax: die ”message”

Example :

open DATA, “+>file.txt” or die “Couldn’t open file file.txt, $!”;

 

Carp Function:

  • The carp function is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name.

Syntax:

carp “message!”;

Example:

open DATA, “+>file.txt” or carp “Error in module!”;

  • If suppose some issue is there in file processing .So it would show the following message.

Error in module! at perlfile1.pl line 3

Cluck Function:

  • The cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including information on the original script.

Syntax:

cluck “message”;

Example:

open DATA, “+>file.txt” or cluck “Error in module!”;

  • If suppose some issue is there in file processing .So it would show the following message.

#Error in module! at sachin.pm line 11

#sachin::function() called at kumar.pm line 13

#kumar::raise() called at test.pl line 3

 

Croak Function:

  • The croak function is the equivalent of die, except that it reports the caller one level up. Like die, this function also exits the script after reporting the error to STDERR:

Syntax:

croak “Definitely didn’t work”;

  • If suppose some issue is there in file processing .So it would show the following message.

Error in module! at kumar.pm line 13

  • As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions.

 

Confess Function:

  • The confess function is like cluck; it calls die and then prints a stack trace all the way up to the origination script.

Syntax:

confess “Failed around about there”;

  • If suppose some issue is there in file processing .So it would show the following message.

#Error in module! at sachin.pm line 11

#sachin::function() called at kumar.pm line 13

#kumar::raise() called at t2.pl line 3

                                                                                                                         

 

Leave a Reply

Your email address will not be published. Required fields are marked *