Essential Tools Module User's Guide : Chapter 9 Error Handling and Reporting : Error Reporting and Error Handling : Using the Error Reporting Classes to Handle Errors
Using the Error Reporting Classes to Handle Errors
The RWxmsg classes may be used in your applications in a number of ways, depending on whether or not your environment enables exceptions.
When Exceptions are Supported
The most common use of the error reporting classes is in your try-catch blocks surrounding the code that could throw an exception. For example, consider the following.
 
RWTRegex<char> r;
try
{
r = RWTRegex<char>(“[a-z]*”);
}
catch (RWRegexErr& e)
{
cout << e.why() << endl; }
In this example, the instantiation (and consequent pattern compilation) could result in a pattern compilation error that would be reported through an exception of type RWRegexErr. Therefore, the instantiation is surrounded in a try-catch block that catches exceptions of type RWRegexErr.
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