|
|
 |
|
|
|
|
104: intArray[j] = j;
105: cout << intArray[ << j << ] okay << end1;
106: }
107: }
108: catch (xBoundary)
109: {
110: cout << Unable to process your input!\n;
111: }
112: cout << Done.\n;
113: return 0;
114: } |
|
|
|
|
|
|
|
|
intArray[0] okay
intArray[1] okay
intArray[2] okay
intArray[3] okay
intArray[4] okay
intArray[5] okay
intArray[6] okay
intArray[7] okay
intArray[8] okay
intArray[9] okay
intArray[10] okay
intArray[11] okay
intArray[12] okay
intArray[13] okay
intArray[14] okay
intArray[15] okay
intArray[16] okay
intArray[17] okay
intArray[18] okay
intArray[19] okay
Unable to process your input!
Done. |
|
|
|
|
|
|
|
|
Analysis: Listing 24.1 presents a somewhat stripped-down Array class, created just to illustrate this simple use of exceptions. On lines 612, a very simple exception class is declared, xBoundary. The most important thing to notice about this class is that there is absolutely nothing that makes it an exception class. In fact, any class, with any name and any number of methods and variables, will do just fine as an exception. What makes this an exception is only that it is thrown, as shown on line 75, and that it is caught, as shown on line 108! |
|
|
|
|
|
|
|
|
The offset operators throw xBoundary when the client of the class attempts to access data outside the array. This is far superior to the way normal arrays handle such a request; they just return whatever garbage happens to be in memory at that location, a sure-fire way to crash your program. |
|
|
|
|
|