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


6.  You can provide a human-readable description of the error by calling one of the two overloaded description functions. The first one provides a description of the current error state. The second one provides a description of the specified error code. You can pass one of these functions to printf or cout to display descriptive error information.
cout << “Error in function: “ << ec.description() << endl;
7.  If you are passing the error object around in your program, you can check the current error state by using the isError function.
if (ec.isError())
   cout << “Error in function: “ << ec.description() << endl;

How It Works

The ErrorClass class maintains an array of error descriptions that are read from an error file. These error messages are sequential starting at 0 and going as high as 100 for this small example. The class definition of the ErrorClass class is provided in Listing 10.1 and the implementation file (.cpp) is given in Listing 10.2. This sample code is also provided on the CD that comes with this book. The important methods of this class will be discussed in turn.

Listing 10.1 ErrorClass Class Definition File

//
// ErrorClass.h - definition of ErrorClass class
//

#include <iostream.h>

#define ERRDESC_SIZE  81
#define MAX_ERRNUM   100

class ErrorClass
{
public:
   ErrorClass();
   virtual ~ErrorClass();

   // Utility methods
   const char* description() const;
   const char* description(int errorNumber) const;

   void currentError(int errorNumber);
   int  currentError(void) const;

   bool isError() const;
   bool loadErrors(const char* errorFile);

protected:
   int  _currentError;
   bool _openError;
   char _errors[MAX_ERRNUM][ERRDESC_SIZE];
};

Listing 10.2 ErrorClass Class Implementation

//
// ErrorClass.cpp - implementation of ErrorClass class
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include “ErrorClass.h”

ErrorClass::ErrorClass()
   : _currentError(-1),
     _openError(false)
{
}

ErrorClass::~ErrorClass()
{
}

const char* ErrorClass::description() const
{
   return description(_currentError);
}

const char* ErrorClass::description(int errorNumber) const
{
   if (_openError)
      return “Error loading errors list”;

   if (_errors[0][0] == ‘\0’ || errorNumber >= MAX_ERRNUM)
      return “”;

   return _errors[errorNumber];
}

void ErrorClass::currentError(int errorNumber)
{
   _currentError = errorNumber;
}

int ErrorClass::currentError() const
{
   return _currentError;
}

bool ErrorClass::isError() const
{
   if (_openError)
      return true;

   return (_currentError >= 0);
}

bool ErrorClass::loadErrors(const char* errorFile)
{
   FILE* fptr = fopen(errorFile, “r”);

   if (fptr == NULL)
   {
      _openError = true;
      return false;
   }
   else
   {
      _openError = false;

      for (int j = 0; !feof(fptr); j++)
      {
	 char buf[ERRDESC_SIZE];

	 // Read the error descriptions from the
	 // error file.  The error descriptions must
	 // be in sequential order.
	 //
	 if (fgets(buf, ERRDESC_SIZE, fptr) != NULL)
	    strcpy(_errors[j], buf);
      }

      fclose(fptr);
   }

   return true;
}


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.