|
|
 |
|
|
|
|
146: cout << Something went wrong, but I've no idea what! << end1;
147: }
148: cout << Done.\n;
149: return 0;
150: } |
|
|
|
|
|
|
|
|
Enter the array size: 5
Too small! Received: 5
Done. |
|
|
|
|
|
|
|
|
Enter the array size: 50000
Too big! Received: 50000
Done. |
|
|
|
|
|
|
|
|
Enter the array size: 12
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
Unable to process your input!
Done. |
|
|
|
|
|
|
|
|
Analysis: Listing 24.2 declares a virtual method in the xSize class, PrintError(), that prints an error message and the actual size of the class. This is overridden in each of the derived classes. |
|
|
|
|
|
|
|
|
On line 140, the exception object is declared to be a reference. When PrintError() is called with a reference to an object, polymorphism causes the correct version of PrintError() to be invoked. The first time through we ask for an array of size 5. This causes the TooSmall exception to be thrown; that is the xSize exception caught on line 140. The second time through we ask for an array of 50,000 and that causes the TooBig exception to be thrown. This is also caught on line 140, but polymorphism causes the right error string to print. When we finally ask for an array of size 12, the array is populated until the xBoundary exception is thrown and caught on line 136. |
|
|
|
|
|