Exceptions
In the preceding sections, you learned that whenever an object of the DB Interface Module has an error to report, its installed error handler is invoked. We examined some very simple error handlers. Here is another which is equally simple, but which has quite a different effect:
 
void exHandler(const RWDBStatus& status) {
status.raise();
}
The raise()method of RWDBStatus throws an exception of type RWExternalErr.
NOTE: RWExternalErr is defined in the Essential Tools Module. Please see the Essential Tools Module User’s Guide for more information.
In the error model of the DB Interface Module, the use of C++ exceptions is optional, and is under your application's control. If you were to install the error handler above into the RWDBManager, you would put your application in a position to use the exception mechanism defined by the C++ standard. How would this affect the example?
 
// Error Handling - version 4
RWDBManager::setErrorHandler(exHandler);
.
.
.
RWCString s; float f;
RWDBReader reader = anotherTable.reader();
while (reader()) {
try {
 
reader >> s >> f; // process s and f
}
catch(RWxmsg x) {
cout << "Error processing anotherTable: "
<< x.why() << endl;
}
}
Once again, our error handling is the same: print a message, exit the loop, and continue. This time we have retained context information, once again at the expense of some inline code, but the inline code is in its own block.
Which technique is best? There is no clear-cut answer; it depends on what fits best with the rest of your application. We recommend that you choose the method you prefer and use it consistently.