Creating Your Own Error Handler
If you want to handle all types of exceptions the same way, such as writing out their messages, you need only one catch block for the base class. For example, suppose that your application uses the combination of predefined and user-defined exception classes shown in
Figure 51.
You could log all the exceptions of type
RWxmsg,
RWTHRxmsg, and
MYxmsg with the simple statement shown in
Example 69.
Example 69 – Implementing a simple error handler
catch(RWxmsg& emsg) {
...
cout << "Oh-oh: " << emsg.why() << endl;
...
}
Because
RWTHRxmsg and
MYxmsg derive from
RWxmsg, this code catches all these exceptions.
If you want to do something different with each exception type, however, you need three separate catch blocks with the most derived type handled first, as shown in
Example 70.
Example 70 – Handling multiple exception types differently
catch(MYxmsg& emsg) {
...
handle my special-purpose thread-compatible exceptions
...
}
catch(RWTHRxmsg& emsg) {
...
handle the generic thread-compatible exceptions
...
}
catch(RWxmsg& emsg) {
...
handle the non-thread-compatible C++ exceptions
...
}
For more information, see the discussion of error handlers in the Essential Tools Module User’s Guide.