Rogue Wave Banner

Click on the banner to return to the user guide home page.

©Copyright 1996 Rogue Wave Software

Exception Architecture

When an exception is thrown a throw operand is passed. The type of the throw operand determines which handlers can catch it. Tools.h++ uses the following hierarchy for throw operands:

xmsg
        RWxmsg
              RWInternalErr
                   RWBoundsErr
              RWExternalErr
                   RWFileErr
                   RWStreamErr
        xalloc
             RWxalloc

As you can see, the hierarchy parallels the error model outlined in the first part of this chapter. This hierarchy assumes the presence of class xmsg, nominally provided by your compiler vendor. This is the class now being considered for standardization by the Library Working Group of the C++ Standardization Committee X3J16 (Document 92-0116). If your compiler does not come with versions of xmsg and xalloc, the Rogue Wave classes RWxmsg and RWxalloc will emulate them for you.

Class xmsg carries a string that can be printed out at the catch site to give the user some idea of what went wrong. This string is formatted and internationalized as described in Chapter 16 (Localizing Messages).

Error Handlers

Tools.h++ uses the macro RWTHROW to throw an exception. If your compiler supports exceptions, this macro resolves by calling a function which throws the exception. If your compiler does not support exceptions, the macro resolves to call an error handler with prototype:

void errHandler(const RWxmsg&);

The default error handler aborts the program. You can change the default handler with the function:

typedef void (*rwErrHandler)(const RWxmsg&);
rwErrHandler rwSetErrHandler(rwErrHandler);

The next example demonstrates how a user-defined error handler works in a compiler that doesn't support exceptions:

#include <rw/rwerr.h>
#include <rw/coreerr.h>
#include <iostream.h>

#ifdef RW_NO_EXCEPTIONS

void myOwnErrorHandler(const RWxmsg& error){
  cout << "myOwnErrorHandler(" << error.why() << ")" << endl;
}

int main(){
  rwSetErrHandler(myOwnErrorHandler);  // Comment out this line 
                             // to get the default error handler.
  RWTHROW( RWExternalErr(RWMessage( RWCORE_GENERIC, 12345, "Howdy!") ));
  cout << "Done." << endl;
  return 0;
}

#else  //RW_NO_EXCEPTIONS

#error This example only for compilers without exception handling

#endif

Previous file Table of Contents Next file