Click Here!
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


The first important method of this class you will use after constructing an ErrorClass object is the loadErrors method. This method first opens the specified error file. Then it reads each line of the file and populates the _errors array. The _errors data member is just an array that can hold up to 100 strings. You might want to implement _errors another way, such as an STL map class that maps error codes to their descriptions. Also, you might want to store your error codes and descriptions in a database.

After the errors are loaded into the array, you can set the current error state by using the overloaded currentError method that takes an integer as its input parameter. You can retrieve the current error state at a later time by calling the other currentError method.

You can check to see whether an error has occurred at any time by calling the isError method. This method returns true if there was an error opening the error file, as represented by the _openError data member, or if the _currentError data member is greater than or equal to 0. It will return false otherwise.

Next, to obtain a human-readable description of an error, you can use the two overloaded description methods. The one that takes no input parameters simply passes the _currentError data member to the description method that takes an integer parameter. This description method returns the string that is stored in the _errors array at the position indicated by the error number.

Listing 10.3 lists the driver program that is included to demonstrate how to use the ErrorClass class.

Listing 10.3 Driver Program for the ErrorClass Class

//
// driver.cpp - Driver for ErrorClass class.
//

#include <iostream.h>
#include <stdlib.h>
#include “ErrorClass.h”
#include “errors.h”

#define ERROR_FILE   “errors.txt”

const ErrorClass& DoSomething();

// Global error object
ErrorClass ec;

void main()
{
   if (ec.loadErrors(ERROR_FILE) == false)
   {
      cout << “Error loading error file” << endl;
      exit(-1);
   }

   ErrorClass ec = DoSomething();

   if (ec.isError())
      cout << “Error in DoSomething: “ << ec.description() << endl;
}

const ErrorClass& DoSomething()
{
   ec.currentError(OPNERR);
   return ec;
}

In order to use ErrorClass, a little preparation has to be done. First, you must create a file that contains all the error descriptions in sequential order. Next, you might want to create a header file that contains macros for each of these errors. This will make your code easier to read and maintain.

Next, you have to construct an ErrorClass object. This object can be global to your program or local to a function that is setting an error.

Then you must load the error descriptions from the error file by calling the loadError method. If the errors are loaded successfully, you can then set the current error using the currentError method and obtain the description of the error using the description method.

The driver code in Listing 10.3 calls a function to perform some action. This function sets the current error to OPNERR and returns a reference to the global ErrorClass object. If you create an ErrorClass object locally in a function, you must pass a copy of the object back to the caller. If you pass back a reference to the local object, your program will crash when the caller tries to use it. This is because the local object was destroyed when control was returned to the caller (that is, when it went out of scope).

Comments

Using a class such as ErrorClass gives you an easy way to provide descriptive information about errors that occur in your program. You can print these descriptions to the standard output or to a log file for later research. I encourage you to change this class to fit the needs of your program.


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.