The most common errors associated with the RWDecimal<T> classes are inexact errors and overflow errors. The Money.h++ C++ library provides a mechanism that lets you customize how these errors are handled.
In the Money.h++ Java library, one error handler lets you define how errors will be dealt with: class DefaultErrorHandler. You can replace DefaultErrorHandler with your own error handler, if desired, but your class must implement the ErrorHandler interface. DefaultErrorHandler is described in the API docs and in Section 8.4.3.
Inexact errors occur when a number cannot be represented exactly with the available number of digits but can be approximated by reducing the number of digits after the decimal. When addition, subtraction, or multiplication result in an inexact error, an RWDecimalInexactErr<T> object is created and passed to an error handler.
By default, an inexact error results in an error message being printed and execution resuming. You can substitute a different action by supplying your own handler. Here is an example in C++:
void myInexactHandler(const RWDecimalInexactErr<RWMP2Int>& errobj) {cerr << "The error message was `" << errobj.msg(); cerr << endl; cerr << "The operands were " << errobj.leftOperand(); cerr << " and " << errobj.rightOperand() << endl; cerr << "Ignoring loss of precision and continuing"; cerr << endl; } yourFunction() {RWDecimal<RWMP2Int>::setInexactHandler(myInexactHandler); . . . }
An overflow error occurs when a number cannot be represented with the available number of digits. For example 10^30 cannot be represented in the RWDecimal<RWMP3Int> class as it has too many digits. When an operation results in an overflow error, an RWDecimalOverflowErr<T> object is created and passed to an error handler.
The default overflow handler will print an error message and throw the error object as a C++ exception. If your compiler does not support exceptions then the RWThrow error handler is called to simulate an exception.
void myOverflowHandler(const RWDecimalOverflowErr<RWMP2Int>& x) {. . Error handling code . . } yourFunction() {RWDecimal<RWMP2Int>::setOverflowHandler(myOverflowHandler) . . .
Money.h++ provides DefaultErrorHandler as the default error handler. This error handler acts globally and is accessible in the MoneyException class. The error handler can be customized in a myriad of ways. It is also possible to make your own error handler by implementing the ErrorHandler interface.
You can use the set methods of DefaultErrorHandler to customize its error-handling behavior by operation or by error type. For example, the first function call below tells the error handler to issue a warning when a PRECISION_EXCEEDED error occurs during a division operation. The second setAction() function tells the error handler to issue an exception if the same error occurs during multiplication.
((DefaultErrorHandler)MoneyException.getErrorHandler()).setAction (MoneyException.PRECISION_EXCEEDED, MoneyException.DIVISION, DefaultErrorHandler.WARNING); ((DefaultErrorHandler)MoneyException.getErrorHandler()).setAction (MoneyException.PRECISION_EXCEEDED, MoneyException.MULTIPLICATION, DefaultErrorHandler.EXCEPTION);
©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.