36: virtual void SetNumber(unsigned long number) {badNumber =
➥number;}
37: private:
38: unsigned long badNumber;
39: };
40:
41: void RangeError::PrintError()
42: {
43: cout << “Number out of range. You used “;
44: cout << GetNumber() << “!!” << endl;
45: }
46:
47: // func. prototypes
48: void MyFunction();
49: unsigned int * FunctionTwo();
50: void FunctionThree(unsigned int *);
51:
52: int main()
53: {
54: try
55: {
56: MyFunction();
57: }
58: // Only one catch required, use virtual functions to do the
59: // right thing.
60: catch (Exception& theException)
61: {
62: theException.PrintError();
63: }
64: return 0;
65: }
66:
67: unsigned int * FunctionTwo()
68: {
69: unsigned int *myInt = new unsigned int;
70: if (myInt == 0)
71: throw OutOfMemory();
72: return myInt;
73: }
74:
75: void MyFunction()
76: {
77: unsigned int *myInt = FunctionTwo();
78: FunctionThree(myInt);
79: cout << “Ok. myInt: “ << *myInt;
80: delete myInt;
81: }
82:
83: void FunctionThree(unsigned int *ptr)
84: {
85: long testNumber;
86: cout << “Enter an int: “;
872 Appendix D
32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 872