{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Chapter5 - Chapter 5 Exceptions What should class functions...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 5 – Exceptions What should class functions do upon detecting errors? Displaying an error message on the screen is not a good idea. Perhaps the application involves a beautiful graphical display, and having an error message pop up in the middle of it isn’t going to look too good. And, in any case, displaying an error message isn’t likely to be very useful. Suppose that we are in fact dealing with a banking application. How is the bank employee using the system supposed to react to a cryptic message to the effect that function such and such doesn’t think much of the arguments that have been passed to it? This doesn’t help. Nor is generally a good idea to fail silently (to just patch things up as best we can and proceed). This simply propagates the error and may make it harder to locate the original source of the problem. Consider, for example, function “move” (from our Date class). If it can’t handle the move requested, it simply sets the date to either January 1, 1900 or December 31, 2099. It should be apparent how, in a banking application, this could lead to some very undesirable effects. If the out of range date is due to a bug in the code using our class, we really should address this issue (i.e. find and fix the bug) and, if it’s just that the program is nearing its expiry date, this too should be dealt with. In general, the appropriate action upon detecting a problem is to report it to the calling function. This function can then deal with the problem in an application-dependent manner or pass the problem on to the function that called it, and so on. This can be accomplished by having functions return a success indication. Function “move”, for example, could be modified as shown below (note that, if we wished, we could also have just changed it from a void function to a bool function). // sets “success” to true if everything goes well and sets “success” false is there are // problems (new date is outside the allowable range). void move (int days, bool &success); This kind of thing gets to be a pain in a hurry, especially if there are multiple possible problems. Note that, if the scheme is to be useful, every call to move must be followed by code which determines whether or not the operation succeeded and takes appropriate action if it didn’t (as shown below). Note that “appropriate action” will typically consist of just passing the program upstairs (i.e. reporting the problem to whoever called us). date.move (days, success); if (!success) { // take appropriate action.. } C++ provides a mechanism which allows us to achieve the same effect much more easily. There is no need to clutter up functions with extra parameters or to test after every function call. Instead problems are automatically passed upstairs until they reach a function which is prepared to deal with them. The mechanism is called “exceptions” (note: JAVA uses exactly the same idea and exactly the same terminology). Imagine a
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
series of function calls as shown below. Function “main” calls function “alpha”, which is turn calls “beta”, which in turn calls “gamma”.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}