General IRI utilities
Public Member Functions | Protected Attributes
CException Class Reference

Generic exception. More...

#include <exceptions.h>

Inheritance diagram for CException:
Inheritance graph
[legend]
Collaboration diagram for CException:
Collaboration graph
[legend]

Public Member Functions

 CException (const std::string &where, const std::string &error_msg)
 Class constructor. More...
 
virtual const std::string & what (void)
 Function to get the error message. More...
 
virtual ~CException () throw ()
 Class destructor. More...
 

Protected Attributes

std::string error_msg
 Exception error message. More...
 

Detailed Description

This class inherits from the std::exception class to provide generic exceptions for for all classes. The error message is provided at construction time with all the necessary information about the error that has generated the exception. When the exception is caught, the what() function may be used to get the error message.

This class should be used as a base class for all other exception classes in any program. This way it will be simple to distinguish between system exceptions and application exceptions.

When throwing exceptions, it is important to free all allocated resources and also to set all the class attributes and variables to a known state. Otherwise the behavior of the program after an exception is thrown could be unpredictible.

Next there is a scketch of a program to catch exceptions:

*   try{
*     user code here
*   }catch(CException &e){
*     handle the exception
*   }
* 

There exist no copy constructor, so if it is necessary to rethrow an exception just use throw; instead of throw e;, because the later tries to create a new CException object and initialize it with the information of the original one.

This code will catch any exception of this class or any inherited class. Several catch statements can be used to catch exceptions of different classes separatelly and handle them in different ways.

Examples:
test_events.cpp, test_eventserver.cpp, and test_time.cpp.

Definition at line 76 of file exceptions.h.

Constructor & Destructor Documentation

CException::CException ( const std::string &  where,
const std::string &  error_msg 
)

The constructor of this class allocates the necessary memory to store the error message. In addition to the error message provided to the constructor, the name of the function, filename and line where the error has ocurred, as well as the text "[Exception caught] - " is pre-appended to label the message as an exception.

Parameters
wherea null terminated string with the information about the name of the function, the source code filename and the line where the exception was generated. This string must be generated by the HERE macro.
error_msga null terminated string that contains the error message. This string may have any valid character and there is no limit on its length.

Definition at line 25 of file exceptions.cpp.

References error_msg.

CException::~CException ( )
throw (
)
virtual

This destructor just frees all the memory allocated to store the error message.

Definition at line 38 of file exceptions.cpp.

Member Function Documentation

const std::string & CException::what ( void  )
virtual

This function is used to get the error message with the information about the error that has generated the exception.

Returns
the error message as a constant string to prevent external modifications of the private attributes of the class.
Examples:
test_events.cpp, test_eventserver.cpp, test_threads.cpp, test_threadserver.cpp, and test_time.cpp.

Definition at line 33 of file exceptions.cpp.

References error_msg.

Field Documentation

std::string CException::error_msg
protected

This attribute is a string which contains the error message with the information about the error that has generated the exception. This message is allocated and initialized at construction time and can not be modified afterwards.

When the exception is caught, the what() function must be used to get the contents of this error message.

Definition at line 90 of file exceptions.h.

Referenced by CEventException::CEventException(), CEventServerException::CEventServerException(), CEventTimeoutException::CEventTimeoutException(), CException(), CLogException::CLogException(), CMutexException::CMutexException(), CThreadException::CThreadException(), CThreadServerException::CThreadServerException(), CTimeException::CTimeException(), and what().


The documentation for this class was generated from the following files: