When Exceptions are Not Supported
Another use of these classes is in setting “call-back” functions in environments in which exceptions are not enabled.
In Windows environments, the default error-handling scheme is to display a Windows message box with the error string, after which the program terminates. In UNIX environments, the error message is written to stderr, and then the program terminates. However, the Essential Tools Module provides a method of setting a user-defined call-back function to be invoked in place of the default error handler whenever such an error occurs.
The function used to set the call-back is named rwSetErrHandler and is declared as follows.
 
rwErrHandler rwexport rwSetErrHandler(rwErrHandler);
The method accepts as an argument a call-back function conforming to the type rwErrHandler. Upon completion, the method returns the previously installed error handler. Call-back functions must have the following signature, as defined by the rwErrHandler typedef.
 
typedef void (*rwErrHandler)(const RWxmsg&);
The function must return void and accept a const reference to an RWxmsg object.
Example:
 
#include <iostream>
#include <rw/compiler.h>
#include <rw/rwerr.h>
#include <rw/coreerr.h>
 
#ifdef RW_NO_EXCEPTIONS
 
void myOwnErrorHandler(const RWxmsg& error)
{
std::cout << "myOwnErrorHandler(" << error.why() << ")" << std::endl;
}
 
int main()
{
// Comment out the following line to get the default error handler.
rwSetErrHandler(myOwnErrorHandler);
RWTHROW( RWExternalErr("Error!"));
std::cout << "Done." << std::endl;
 
return 0;
}
 
#else // RW_NO_EXCEPTIONS
 
#include <stdio.h>
 
int main ()
{
printf ("This example is only for C++ "
"compilers with no exceptions support.\n");
}
 
#endif