Chapter 7. Exception Handling
During the implementation of our Triangular_iterator class in Section 4.6, we realized that the iterator can potentially fall into an error state. The _index data member might become set to a value greater than the maximum number of elements stored in the static vector of the Triangular class. On the face of it, this seems an unlikely thing to happen. If it does happen, however, that's bad because the program that is using the value is likely to fail. It's also bad because the programmer using the iterator class has no easy way to recognize or resolve the problem.
As the designer of the iterator class, we can recognize the problem: The iterator is no longer in a valid state and must no longer be used within the program. But we do not know how serious the problem is to the overall program. Only the user of the iterator has that knowledge. Our job is to notify the user. To do this, we use the C++ exception handling facility, the topic of this chapter.
|