![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
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. Youll 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. Isnt 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 screenperror. 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
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.
|
![]() |
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.
|