Error class that incorporates additional information. More...
#include <ErrorObj.hpp>
Public Types | |
enum | Verbosity { NON_VERBOSE, VERBOSE } |
ErrorObj::Verbosity denotes whether the output of | |
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). |
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
Definition at line 144 of file ErrorObj.hpp.
ErrorObj::Verbosity denotes whether the output of what(const Verbosity)
is more or less detailed.
NON_VERBOSE
is always assumed to be zero. 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.
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.
[in] | error_message | Error 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.
[in] | error_message | Error message that is returned when calling getErrorMessage() or what(). |
[in] | class_name | Class 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.
[in] | error_message | Error message that is returned when calling getErrorMessage() or what(). |
[in] | class_name | Class name that is returned when calling getClassName(). |
[in] | function_name | Function name that is returned when calling getFunctionName(). |
[in] | error_code | Error 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.
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).
Definition at line 112 of file ErrorObj.cpp.
int TRTK::ErrorObj::getErrorCode | ( | void | ) | const |
Returns the stored error Code.
Definition at line 120 of file ErrorObj.cpp.
std::string TRTK::ErrorObj::getErrorMessage | ( | void | ) | const |
Returns the stored 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).
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).
[in] | class_name | Class 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.
[in] | error_code | Error 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.
[in] | error_message | Error 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).
[in] | function_name | Function 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.
[in] | verbose | Specifies the level of verbosity. |
Definition at line 231 of file ErrorObj.cpp.
const char * TRTK::ErrorObj::what | ( | ) | const throw () [virtual] |
Returns the error message.
[in] | value | This 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. |
Definition at line 216 of file ErrorObj.cpp.
Documentation generated by Doxygen