SourceForge.net Logo
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

EkError Class Reference

Base class for error objects. More...

#include <EkError.h>

Inheritance diagram for EkError:

[legend]
Collaboration diagram for EkError:
[legend]
List of all members.

Public Methods

 EkError (const char *component, int errcode, const char *filename="< file name unknown >", int line=0)
 EkError ()
 EkError (const EkError &)
virtual ~EkError () throw ()
EkError & operator= (const EkError &)
bool operator< (const EkError &rhs) const
bool operator== (const EkError &rhs) const
bool isA (const char *component, int errcode) const
const SPmtStringcomponent () const
const int & errcode () const
const int & line () const
const SPmtStringfilename () const
const time_t & timestamp () const
bool isInitialized () const
EkError & setMsg (const SPmtString &)
EkError & setMsg (const char *)
virtual const char * getMsg () const
virtual const char * what () const throw ()
void print (ostream &os) const

Static Public Attributes

bool msUseDate = true
bool msUseLines = true

Protected Attributes

SPmtString mMyComponent
int mMyCode
int mMyLine
SPmtString mMyFileName
time_t mMyTimestamp
SPmtString mMyMessage
bool mInitialized

Friends

class EkErrp

Detailed Description

Base class for error objects.

Specialized error object classes may be derived from this base class. Such classes would add data members to hold domain-specific error information, accessor functions to retrieve such data, and their own getMsg() function to produce a comprehensible human-readable message that describes the error.

This class may also be used by itself to create minimal, unspecialized error objects.

EkError objects characterize errors with the following information:

component - a SPmtString that names the software component that created the error. code - a numeric error code. Software components will probably create an enum to hold their various error codes. line - source file line number where the error object was created. filename - name of source file timestamp - time and date when error obejct was created

The component, code, line and fileName are all parameters to the EkError constructor. The WHERE macro provides a convenient shorthand for providing the the line and filename values to the constructor

The getMsg() function returns a text SPmtString that describes the error. getMsg() is a virtual function, and it is expected that derived classes will redefine it to produce appropriate descriptions for the errors they encompass. The default implementation of getMsg() is pretty simpleminded. This virtual function allows general purpose error logging and reporting code to be written that handle just EkErrors or EkErrps without needing to know the actual derived classes of the errors.

The default constructor allows uninitialized error obejcts to be created. These are identifiable byuse of the "isInitialized()" method. Debug versions will catch attempted use of uninitialized error objects by failure of "assert()" calls. If the "assert" mechanism is defeated, as in release code, uninitialized error objects may appear in error logs and displays. They are characterized by component code "EkError" and error code 0.

Things to consider when creating a derived error class:

  • Make filename and line the last two parameters of the constructor, Default them to "<File name unknown>" and 0.
  • Create a getMsg() function that creates a SPmtString in myMessage that describes the error.
  • If your class adds data members that are pointers, make sure to provide a copy constructor, an operator=, and a destructor that do the right thing.
Example: (boring bits elided by ellipses: "...")

OrderError - a derived error class containing an order number

    class OrderError : public EkError
    {
    public:

        OrderError(const char* component, int errcode, OrderId order, 
                   const char* filename = "<file name unknown>", int line = 0);

        OrderId getOrderId();

        // produces OrderError-specific message
        virtual const char* getMsg(); 

        static const char* const className;

     protected:
         OrderId myOrderId;
     } // end class OrderError

     // Static initialization
     const char* const OrderError::classname = "OrderError";

     // Constructor
     OrderError::OrderError(const char* component, int errcode, OrderId order,
                            const char* filename, int line)
     : EkError(component, errcode, filename, line), myOrderId(order)
     {
         myClassName = OrderError::className;
     }

    // someFunction - returns an EkErrp that may point to one
    //                of several kinds of errors
    EkErrp someFunction(...)
    {
        ...
        if(...error discovered...)
        {
            EkErrp ep(new OrderError(ORDER_ENTRY, BAD_COUNT, OrderNo, WHERE));
            return ep;
        }
        ...
    }

    // Call someFunction and handle returned errors
    EkErrp ep = someFunction();
    if(ep)
    {
        // log and display message
        errorLog.write(ep);
        statusLine.displayMessage(ep->getMessage());

        // Cancel order if message type was OrderError 
        if(ep->getClassName() == OrderError::className)
            cancelOrder(((OrderError*)ep)->getOrderId);
     }


Constructor & Destructor Documentation

EkError::EkError const char *    component,
int    errcode,
const char *    filename = "< file name unknown >",
int    line = 0
 

EkError::EkError  
 

EkError::EkError const EkError &   
 

EkError::~EkError   throw () [virtual]
 


Member Function Documentation

const SPmtString & EkError::component   const
 

const int & EkError::errcode   const
 

const SPmtString & EkError::filename   const
 

const char * EkError::getMsg   const [virtual]
 

bool EkError::isA const char *    component,
int    errcode
const
 

bool EkError::isInitialized   const
 

const int & EkError::line   const
 

bool EkError::operator< const EkError &    rhs const [inline]
 

EkError & EkError::operator= const EkError &   
 

bool EkError::operator== const EkError &    rhs const [inline]
 

void EkError::print ostream &    os const
 

EkError & EkError::setMsg const char *   
 

EkError & EkError::setMsg const SPmtString  
 

const time_t & EkError::timestamp   const
 

const char * EkError::what   const throw () [virtual]
 


Friends And Related Function Documentation

friend class EkErrp [friend]
 


Member Data Documentation

bool EkError::mInitialized [protected]
 

int EkError::mMyCode [protected]
 

SPmtString EkError::mMyComponent [protected]
 

SPmtString EkError::mMyFileName [protected]
 

int EkError::mMyLine [protected]
 

SPmtString EkError::mMyMessage [protected]
 

time_t EkError::mMyTimestamp [protected]
 

bool EkError::msUseDate = true [static]
 

bool EkError::msUseLines = true [static]
 


The documentation for this class was generated from the following files:
Last Updated: March 12, 2003 (rlr)
Copyright © 2003 Eastman Kodak Company All Rights Reserved.