Perl Error Trapping
Contents |
Perl IF..ELSE.. Perl Loopings Perl Operators Perl Files & I/O Regular Expressions Perl Subroutines Perl Formats Perl Error Handling Perl Coding Standard Advanced PERL Perl Sockets Writing Perl Modules Object Oriented Perl Database Management CGI Programming PERL References Perl perl error handling eval Functions Selected Reading Computer Glossary Who is Who Copyright © 2014 by tutorialspoint Home
Perl Error Variable
References About TP Advertising Error Handling in PERL Advertisements You can identify and trap an error in a number of different
Perl Catch Die
ways. Its very easy to trap errors in Perl and then handling them properly. Here are few methods which can be used. Using if The if statement is the obvious choice when you need to
Die In Perl Script
check the return value from a statement; for example: if (open(DATA,$file)) { ... } else { die "Error: Couldn't open the file $!"; } Here variable $! returns the actual error message Alternatively, we can reduce the statement to one line in situations where it makes sense to do so; for example: die "Error: Something went wrong\n" if (error()); Using unless The unless function is the logical opposite to if: statements exception handling in perl try catch can completely bypass the success status and only be executed if the expression returns false. For example: unless(chdir("/etc")) { die "Error: Can't change directory!: $!"; } The unless statement is best used when you want to raise an error or alternative only if the expression fails. The statement also makes sense when used in a single-line statement: die "Error: Can't change directory!: $!" unless(chdir("/etc")); Here we die only if the chdir operation fails, and it reads nicely. Using the Conditional Operator For very short tests, you can use the conditional operator: print(exists($hash{value}) ? 'There' : 'Missing',"\n"); It's not quite so clear here what we are trying to achieve, but the effect is the same as using an if or unless statement. The conditional operator is best used when you want to quickly return one of two values within an expression or statement. The Warn Function The warn function just raises a warning, a message is printed to STDERR, but no further action is taken. chdir('/etc') or warn "Can't change directory"; The 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. chdir('/etc') or die "Can't change directory"; Reporting Errors Within Modules There are
A • B • C • D • E F • G • H • I • L M • N • O • P • S T • U • X die Perl 5 version perl error handling best practices 24.0 documentation Go to top Show recent pages Home > Language reference > perl die exit code Functions > die Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled, or perl die vs croak are running a non-JavaScript capable web browser. To get the best experience, please enable JavaScript or download a modern web browser such as Internet Explorer 8, Firefox, Safari, or Google Chrome. Recently http://www.tutorialspoint.com/perl/perl_error_handeling.htm read die Perl functions A-Z | Perl functions by category | The 'perlfunc' manpage die LIST die raises an exception. Inside an eval the error message is stuffed into $@ and the eval is terminated with the undefined value. If the exception is outside of all enclosing evals, then the uncaught exception prints LIST to STDERR and exits with a non-zero value. If you http://perldoc.perl.org/functions/die.html need to exit the process with a specific exit code, see exit. Equivalent examples: die "Can't cd to spool: $!\n" unless chdir '/usr/spool/news'; chdir '/usr/spool/news' or die "Can't cd to spool: $!\n"If the last element of LIST does not end in a newline, the current script line number and input line number (if any) are also printed, and a newline is supplied. Note that the "input line number" (also known as "chunk") is subject to whatever notion of "line" happens to be currently in effect, and is also available as the special variable $. . See $/ in perlvar and $. in perlvar. Hint: sometimes appending ", stopped" to your message will cause it to make better sense when the string "at foo line 123" is appended. Suppose you are running script "canasta". die "/etc/games is no good"; die "/etc/games is no good, stopped";produce, respectively /etc/games is no good at canasta line 123. /etc/games is no good, stopped at canasta line 123.If the output is empty and $@ already contains a value (typically from a previous eval) that value is reused after appending "\t...propagated" . This is
I: Basic Perl 01-Getting Your Feet Wet 02-Numeric and String Literals 03-Variables 04-Operators 05-Functions 06-Statements 07-Control Statements 08-References Part II: Intermediate Perl 09-Using https://affy.blogspot.com/p5be/ch13.htm Files 10-Regular Expressions 11-Creating Reports Part III: Advanced Perl 12-Using Special Variables 13-Handling Errors and Signals 14-What Are Objects? 15-Perl Modules 16-Debugging Perl 17-Command line Options Part IV: Perl and the Internet 18-Using Internet Protocols ftplib.pl 19-What is CGI? 20-Form Processing 21-Using Perl with Web Servers 22-Internet Resources Appendixes A-Review Questions B-Glossary C-Function perl error List D-The Windows Registry E-What's On the CD? 13 - Handling Errors and Signals Most of the examples in this book have been ignoring the fact that errors can and probably will occur. An error can occur because the directory you are trying to use does not exist, the perl error handling disk is full, or any of a thousand other reasons. Quite often, you won't be able to do anything to recover from an error, and your program should exit. However, exiting after displaying a user-friendly error message is much preferable than waiting until the operating system or Perl's own error handling takes over. After looking at errors generated by function calls, we'll look at a way to prevent certain normally fatal activities - like dividing by zero - from stopping the execution of your script; this is by using the eval() function. Then, you'll see what a signal is and how to use the %SIG associative array to create a signal handling function. Checking for ErrorsThere is only one way to check for errors in any programming language. You need to test the return values of the functions that you call. Most functions return zero or false when something goes wrong. So when using a critical