iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
nrt::exception::Exception Class Reference

#include <nrt/Core/Typing/Exception.H>

Exception base class for NRT. This is essentially the same as std::exception but in the nrt namespace.

It is tricky to ensure that constructor, destructor, and what() functions of derived exceptions won't throw. The recommended definition of a derived exception is as follows:

class MyException : public nrt::exception::Exception
{
public:
MyException(std::string const & msg, int const some_value) throw()
: nrt::exception::Exception("Error MyException, and further error while constructing the exception")
{
try {
// Do things that could throw:
std::ostringstream os;
os << "Error MyException with message [" << msg << "] and value [" << some_value;
whatstring = os.str();
// At last set whatptr when you know nothing will throw anymore:
whatptr = whatstring.c_str();
}
catch (...) { } // oops, failed assembling error message, the one specified in the initializer
// list will then be used. Here we don't throw.
}
virtual ~MyException() { }
private:
std::string whatstring;
};

If you want a more complicated initializer list, then use this fancy syntax for your constructor:

MyException(std::string const & msg, ComplexObject const & obj) throw()
try : nrt::exception::Exception("Error MyException, and further error while constructing the exception"),
myobj(obj)
{
// possibly fiddle more with msg, myobj, etc and create a whatstring data member
// At last set whatptr when you know nothing will throw anymore:
whatptr = whatstring.c_str();
}
catch (...) { } // nrt::Exception::Exception() does not throw, so here just cleanup your other objects

See here for more info: http://www.gotw.ca/gotw/066.htm

Examples:
tests/test-Component.C, and tests/test-ImageCopy.C.

Definition at line 121 of file Exception.H.

Inheritance diagram for nrt::exception::Exception:
nrt::exception::BadEnumStringException nrt::exception::BadParameter nrt::exception::ConversionException nrt::exception::DenseDataBoundsException nrt::exception::DisplayListManagerException nrt::exception::FactoryException nrt::exception::FatalException nrt::exception::GenericBagException nrt::exception::PointCloud2CorrespondenceException nrt::exception::PointCloud2FieldsException nrt::exception::PointCloud2FilterException nrt::exception::PointCloud2SubsetException nrt::exception::SerializableException nrt::exception::SparseDataNotFoundException nrt::exception::TextureManagerException

Public Member Functions

 Exception () throw ()
 Constructor with a default non-specific error message.
 
 Exception (char const *msg) throw ()
 Constructor with a given error message. More...
 
 Exception (Exception const &e) throw ()
 Copy constructor.
 
virtual ~Exception () throw ()
 Virtual destructor for safe inheritance.
 
Exceptionoperator= (Exception const &e) throw ()
 Assignment.
 
char const * what () const throw ()
 Return a C string describing the error. More...
 

Protected Attributes

char const * whatptr
 Pointer to a message, returned by what()
 

Constructor & Destructor Documentation

nrt::exception::Exception::Exception ( char const *  msg)
throw (
)

Constructor with a given error message.

The message is given as an old-school C string to ensure that the exception constructor won't throw. In the constructor we just set whatptr to that C string. Typically that string would be a plain string message in your code (i.e., not dynamically allocated, temporary object, etc).

Member Function Documentation

char const* nrt::exception::Exception::what ( ) const
throw (
)

Return a C string describing the error.

In derived classes, do not overload what(), but instead just set whatptr to point to your error message.

Examples:
tests/test-Component.C, tests/test-PointCloud2OpenNI.C, tests/test-ResultsSyntax.C, and tests/test-stream.C.

Referenced by nrt::ParameterCore< T >::set().


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