Handling Errors and Exceptions 737
20
71: if (size > 30000)
72: throw xTooBig(size);
73: if (size < 1)
74: throw xNegative(size);
75: if (size < 10)
76: throw xTooSmall(size);
77:
78: pType = new int[size];
79: for (int i = 0; i < size; i++)
80: pType[i] = 0;
81: }
82:
83:
84: int& Array::operator[] (int offSet)
85: {
86: int size = GetitsSize();
87: if (offSet >= 0 && offSet < size)
88: return pType[offSet];
89: throw xBoundary();
90: return pType[0];
91: }
92:
93: const int& Array::operator[] (int offSet) const
94: {
95: int size = GetitsSize();
96: if (offSet >= 0 && offSet < size)
97: return pType[offSet];
98: throw xBoundary();
99: return pType[0];
100: }
101:
102: int main()
103: {
104: try
105: {
106: Array intArray(9);
107: for (int j = 0; j < 100; j++)
108: {
109: intArray[j] = j;
110: cout << “intArray[“ << j << “] okay...” << endl;
111: }
112: }
113: catch (Array::xBoundary)
114: {
115: cout << “Unable to process your input!” << endl;
116: }
117: catch (Array::xZero theException)
118: {
LISTING20.7 continued