Public Types | Public Member Functions

TRTK::ErrorObj Class Reference

Error class that incorporates additional information. More...

#include <ErrorObj.hpp>

List of all members.

Public Types

enum  Verbosity { NON_VERBOSE, VERBOSE }
 

ErrorObj::Verbosity denotes whether the output of what(const Verbosity) is more or less detailed.

More...

Public Member Functions

 ErrorObj ()
 ErrorObj (const std::string error_message)
 ErrorObj (const std::string error_message, const std::string class_name)
 ErrorObj (const std::string error_message, const std::string class_name, const std::string function_name, const int error_code=0)
virtual ~ErrorObj () throw ()
virtual const char * what () const throw ()
 Returns the error message.
virtual const char * what (const Verbosity=NON_VERBOSE) const throw ()
 Returns the error message.
std::string getClassName (void) const
 Returns the stored class name (this should be the name of the class in which ErrorObj was thrown).
int getErrorCode (void) const
 Returns the stored error Code.
std::string getErrorMessage (void) const
 Returns the stored error message.
std::string getFunctionName (void) const
 Returns the stored function name (this should be the name of the function in which ErrorObj was thrown).
void setClassName (const std::string class_name)
 Sets the class name (this should be the name of the class in which ErrorObj is thrown).
void setErrorCode (const int error_code)
 Sets the error code.
void setErrorMessage (const std::string error_message)
 Sets the error message.
void setFunctionName (const std::string function_name)
 Sets the function name (this should be the name of the function in which ErrorObj is thrown).

Detailed Description

Error class that incorporates additional information.

Compared to std::exception ErrorObj allows to store additional information such as the name of the class and/or function in which is was thrown. In fact, ErrorObj is derived from the standard exception class, that is, catching can be done as for most components of the standard library like

 try
 {
     ...
 }
 catch (std::exception & e)
 {
     ...
 }

The virtual member function what() works as expected.

If you want to be able to get access to possibly further stored information, you must catch a reference to ErrorObj like

 try
 {
     ...
 }
 catch (TRTK::ErrorObj & e)
 {
     ...
 }

Now you are able to access information like the class or function name, for instance. You can use the overloaded function of what() to get a more verbose error message, that is, in addition to the pure error message all other available information is returned.

Here is a more elaborated example of how to use ErrorObj

 #include <iostream>
 #include <TRTK/ErrorObj.hpp>

 using namespace TRTK;

 class A
 {
 public:
     void func()
     {
         // ...
         ErrorObj error("An error occurred.");
         error.setClassName("A");
         error.setFunctionName("func");
         throw error;
     }
 };

 class B
 {
 public:
     void func()
     {
         A a;
         a.func();
     }
 };

 int main()
 {
     B b;

     try
     {
         b.func();
     }
     catch(ErrorObj & error)
     {
         std::cout << error.what(ErrorObj::VERBOSE);
     }

     return 0;
 }

The output is

 Error:
   - Class: A
   - Function: func
   - Error message: An error occurred.
   - Error Code: 0
Warning:
When using exceptions, you need to compile all of your code using the same compiler, since exceptions do not work across compiler bounds! Keep this in mind, when building this class in a static or dynamic library.
Author:
Christoph Haenisch
Version:
0.7.1
Date:
last changed on 2011-09-09

Definition at line 144 of file ErrorObj.hpp.


Member Enumeration Documentation

ErrorObj::Verbosity denotes whether the output of what(const Verbosity) is more or less detailed.

Note:
NON_VERBOSE is always assumed to be zero.
Enumerator:
NON_VERBOSE 

return error message only when calling what()

VERBOSE 

return all information available when calling what()

Definition at line 167 of file ErrorObj.hpp.


Constructor & Destructor Documentation

TRTK::ErrorObj::ErrorObj (  )

Constructs an empty ErrorObj.

Definition at line 35 of file ErrorObj.cpp.

TRTK::ErrorObj::ErrorObj ( const std::string  error_message )

Constructs an ErrorObj.

Parameters:
[in]error_messageError message that is returned when calling getErrorMessage() or what().

Definition at line 47 of file ErrorObj.cpp.

TRTK::ErrorObj::ErrorObj ( const std::string  error_message,
const std::string  class_name 
)

Constructs an ErrorObj.

Parameters:
[in]error_messageError message that is returned when calling getErrorMessage() or what().
[in]class_nameClass name that is returned when calling getClassName().

Definition at line 64 of file ErrorObj.cpp.

TRTK::ErrorObj::ErrorObj ( const std::string  error_message,
const std::string  class_name,
const std::string  function_name,
const int  error_code = 0 
)

Constructs an ErrorObj.

Parameters:
[in]error_messageError message that is returned when calling getErrorMessage() or what().
[in]class_nameClass name that is returned when calling getClassName().
[in]function_nameFunction name that is returned when calling getFunctionName().
[in]error_codeError code that is returned when calling getErrorCode().

Definition at line 89 of file ErrorObj.cpp.

TRTK::ErrorObj::~ErrorObj (  ) throw () [virtual]

Destroys ErrorObj.

Definition at line 104 of file ErrorObj.cpp.


Member Function Documentation

std::string TRTK::ErrorObj::getClassName ( void   ) const

Returns the stored class name (this should be the name of the class in which ErrorObj was thrown).

Returns:
Returns the class name.

Definition at line 112 of file ErrorObj.cpp.

int TRTK::ErrorObj::getErrorCode ( void   ) const

Returns the stored error Code.

Returns:
Returns the error code.

Definition at line 120 of file ErrorObj.cpp.

std::string TRTK::ErrorObj::getErrorMessage ( void   ) const

Returns the stored error message.

Returns:
Returns the error message.

Definition at line 128 of file ErrorObj.cpp.

std::string TRTK::ErrorObj::getFunctionName ( void   ) const

Returns the stored function name (this should be the name of the function in which ErrorObj was thrown).

Returns:
Returns the function name.

Definition at line 136 of file ErrorObj.cpp.

void TRTK::ErrorObj::setClassName ( const std::string  class_name )

Sets the class name (this should be the name of the class in which ErrorObj is thrown).

Returns:
Returns the value stored in ErrorObj.
Parameters:
[in]class_nameClass name that is returned when calling getClassName().

Definition at line 155 of file ErrorObj.cpp.

void TRTK::ErrorObj::setErrorCode ( const int  error_code )

Sets the error code.

Parameters:
[in]error_codeError code that is returned when calling getErrorCode().

Definition at line 166 of file ErrorObj.cpp.

void TRTK::ErrorObj::setErrorMessage ( const std::string  error_message )

Sets the error message.

Parameters:
[in]error_messageError message that is returned when calling getErrorMessage().

Definition at line 177 of file ErrorObj.cpp.

void TRTK::ErrorObj::setFunctionName ( const std::string  function_name )

Sets the function name (this should be the name of the function in which ErrorObj is thrown).

Parameters:
[in]function_nameFunction name that is returned when calling getFunctionName().

Definition at line 188 of file ErrorObj.cpp.

const char * TRTK::ErrorObj::what ( const Verbosity  verbose = NON_VERBOSE ) const throw () [virtual]

Returns the error message.

Depending on the input argument this method returns the pure error message or additional information.

Parameters:
[in]verboseSpecifies the level of verbosity.
See also:
Verbosity and what()

Definition at line 231 of file ErrorObj.cpp.

const char * TRTK::ErrorObj::what (  ) const throw () [virtual]

Returns the error message.

Parameters:
[in]valueThis can be an arbitrary value that is returned when calling getValue(). Only the pure error message is returned and no further information about the class, function etc. in which the object was thrown.
Returns:
Error message.
See also:
what(const Verbosity verbose = NON_VERBOSE)

Definition at line 216 of file ErrorObj.cpp.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines