Introduction
Previous chapters introduced C++ flow-of-control statements and variable declarations. We have had a taste of object-oriented programming and created data structures out of dynamic variables. In this chapter, we turn our attention to how C++ can help a developer handle situations that arise when something unexpectedly goes wrong in a program.
An invalid number input by the user, an unanticipated timeout awaiting a response, and a logic error are all examples of events in a program. Some of these events, such as the input error, may occur so frequently or predictably that they must be anticipated and handled, or else the program will be unusable. Other events, such as the timeout, happen rarely, and never when the program and the system within which it runs are working perfectly. Still other events, such as the logic error, are never meant to happen at all, but sometimes they do anyway.
The user input error event is an expected event. It is handled with specific...