Click Here For A Free vLab!
home account info subscribe login search FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
[an error occurred while processing this directive]
Previous Table of Contents Next


Technique

In order to handle errors that are returned from functions, you have to know what those error values mean. This is easy if the return values are coming from functions you have created. But what if you are using the Standard C Library and wish to report errors returned from functions in this library in a manner that is understandable to everyone?

You could look up the error code for each function you are calling and insert if statements in your code to test for each error. Then, you could write a descriptive message to the standard output or a log file based on the error code.

As you might imagine, this is quite cumbersome. You’ll need one if statement for each error code. To make things easier, you could just write the error code to your log file or to the screen and then look up the error manually.

Again, this still requires a lot of work. Isn’t there a better way? Fortunately, there is. The Standard C Library maintains a global variable called errno to report error codes. Also, it provides a function to print a descriptive error to the screen—perror. The Standard C Library also provides the strerror function to enable you to create human-readable error messages. Finally, a number of macros are provided to help make error messages from Standard C Library functions more readable. The following sections will show you how to use each of these facilities and how they work.

Steps

1.  Clear the global errno variable by setting it to 0.
2.  Call a Standard C Library function that might generate an error.
3.  Test the return value of the function you called to see if it was successful or failed.
4.  Call perror if the Standard C Library function failed.
5.  Pass a string to perror that you want to be displayed along with the description of the return value.
perror(“Could not open file”);
6.  Call strerror to create an error message string that you can print out to the screen or write to a file.
7.  Pass the errno variable for which you want to create a description to strerror.
printf(“Could not open file: %s\n”, strerror(errno));
8.  Use any of the special built-in macros provided in ANSI C implementations to help you locate the error at runtime. See Table 10.2 for a list of the provided macros and what they mean.
Table 10.2 Built-in ANSI C Macros
Macro Description
__DATE__ The date the source file was compiled in the form Mmm dd yyyy.
For example, Oct 4 1998.
__FILE__ The name of the current source code file.
__LINE__ Decimal integer representing the current line number in the source file.
__STDC__ This macro can be used to determine whether the compiler is ANSI C compliant.
__TIME__ The time the source file was compiled in the form hh:mm:ss.
9.  The following is a sample code snippet that attempts to open a file that does not exist.
#include <stdio.h>
#include <string.h>
#include <errno.h>

void main()
{
   FILE* fptr;

   errno = 0;
   fptr = fopen(“NoSuchFile”, “r”);

   if (fptr == NULL)
   {
      perror(“(perror)   - Could not open NoSuchFile”);

      printf(“\n%s \n\t file:  %s\n\t date:  %s\n\t time:
      ⇒%s\n\t error: %s\n”,
	     “(strerror) - Could not open NoSuchFile “,
	     __FILE__, __DATE__, __TIME__, strerror(errno));
   }
   else
   {
      fclose(fptr);
   }
}
10.  The following is the output from this sample program:
(perror)  - Could not open NoSuchFile: No such file or directory

(strerror) - Could not open NoSuchFile
	 file:  printerr.c
	 date:  Oct  4 1998
	 time:  13:31:24
	 error: No such file or directory

How It Works

The Standard C Library functions use the global variable errno to hold error codes. These error codes are typically defined in the errno.h header file. All error codes are positive integers and the Standard C Library routines should not clear the errno variable. That is the reason for step 1, the clearing of errno.

For efficiency and performance purposes, the values of errno are usually stored in an array of string pointers called sys_errlist. The maximum number of elements in the sys_errlist array is indicated by the value of a global variable named sys_nerr. The names of the sys_errlist array and sys_nerr variable might be slightly different depending on the compiler you are using.

To obtain the description of the error value in errno, perror and strerror retrieve the string at the position in the array indicated by errno. They are able to do this because the sys_errlist array is indexed by the errno variable.

The preceding sample code attempts to open a file that does not exist. If the returned file pointer is null, which it should be, the code proceeds to print the error. The error is printed in two different ways. First, perror is used to print a short message along with the error. Next, strerror along with some of the predefined ANSI C macros are used to print a more descriptive message. The perror function prints its description to stderr, which is usually defined to be the console window.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.