![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
[an error occurred while processing this directive]
How It Works The addRecord method belongs to the ProductDatabase class, which is a class that contains functions to open and close a database as well as add, retrieve, and delete records. This class maintains two databases. If the first database fails, the second one will be used. In this simple example, the databases are just flat files. To create an instance of the class, the code must pass the names of the databases to be used for the primary and secondary databases. The constructor for the ProductDatabase class does not open the databases; it just stores the names in an array. This is a real-time system, so the databases should not be open longer than they have to be. Next, the code creates an instance of the Product class. This is a very simple class that represents a product to be added to the database. Then call the addRecord method passing the enumerated value Primary and the product that was just created. The Primary enumerated type tells addRecord which database to use when adding the record. The addRecord method first checks to see whether the database is already open by checking to see whether the database handle member variable, _hDatabase, is null. The addRecord method can be called from a method that already has the database open. For this reason, you dont need to open it if it is already open. Next, addRecord attempts to open the database indicated by the enumerated type that was passed to this function. As mentioned previously, the database names are kept in an array. The enumerated type indicates the position in the array of the database to open. If the open operation fails, -1 is stored in the local integer, retVal, which will be returned to the caller. If the database was opened, addRecord tries to find a duplicate record in the database. Because this system allows duplicate records to be added to the database, this operation is just for reporting purposes. In systems that do not allow duplicate records, this would be an error condition. If a duplicate record is found, retVal is set to a value of 1. This is a warning code that tells the caller that a duplicate record was added to the database. Then, an attempt is made to add the record to the database by calling a database-specific function to add the record. In this sample code, the database is a flat file, so simply call fprintf to write the database record to the file. If this function fails, retVal is set to -2. You might wish to set retVal to the return value of the database-specific function you decide to use. If the database was opened in the addRecord method and not by the caller, the database is closed. Finally, retVal is returned to the caller to indicate the success or failure of the add operation. Going back to the calling function, main, youll see that the code tests the value returned from the call to addRecord and displays a message indicating the success or failure of the operation. If the return value indicates that the Primary database could not be opened (-1), the program attempts the operation again on the Secondary database. The preceding code demonstrates how to handle errors in your programs. To see this code in action, compile and run the sample code for this chapter that is included on the CD that comes with this book. Comments One thing to note about returning error codes from functions is that these return values are the decision of the developer of that particular function. This means return values from a function developed by one person might be totally different values than a function developed by another person. As you can see, the situation can become very confusing very fast. For that reason, it is a good idea to develop a set of standards for return values that all developers on a project can follow. One possibility is to use negative return values to indicate an error condition, 0 to indicate success, and positive values to indicate success with warnings. But this still doesnt fix the problem completely. A return value of -1 from one method might mean something different than a return value of -1 from another method. Therefore, it is good practice to create your error codes as #defines or constants and group them all in a single header file that is used in every file in your application. That way, return values mean the same thing no matter which method or function they come from. As an alternative, you might wish to store your error and success codes along with their descriptions in a database that can be accessed by reporting programs. You can then generate meaningful reports from log files that contain only numeric codes. Another standard that can be employed is using an error class for the return value of a function or method. This error class can take care of maintaining return values and providing methods that create human-readable versions of these values. An example of such a class will be discussed later in this chapter. 10.2 Use the standard C library functions perror and strerror and the predefined C macros to report runtime errors in my programs?Problem I want to be able to handle errors that occur in the Standard C Library functions I am calling in my program. Is there anything provided by the Standard C Library that will help me do this?
|
![]() |
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.
|