- A catchstatement is a routine that has a signature of the type of exception it han-
dles. It follows a tryblock and acts as the receiver of exceptions raised within the
tryblock. - An exception is an object and can contain any information that can be defined
within a user-created class. - Exception objects are created when the program invokes the keyword throw.
- In general, exceptions should be passed by reference. If you don’t intend to modify
the contents of the exception object, you should pass a constreference. - Yes, if you pass the exception by reference.
- catchstatements are examined in the order they appear in the source code. The
first catchstatement whose signature matches the exception is used. In general, it
is best to start with the most specific exception and work toward the most general. - catch(...)catches any exception of any type.
- A breakpoint is a place in the code where the debugger stops execution.
Exercises
- The following is one possible answer:
0: #include
1: using namespace std;
2: class OutOfMemory {};
3: int main()
4: {
5: try
6: {
7: int *myInt = new int;
8: if (myInt == 0)
9: throw OutOfMemory();
10: }
11: catch (OutOfMemory)
12: {
13: cout << “Unable to allocate memory!” << endl;
14: }
15: return 0;
16: } - The following is one possible answer:
1: #include
2: #include <stdio.h>
3: #include <string.h>
4: using namespace std;
5: class OutOfMemory
6: {
7: public:
8: OutOfMemory(char *);
868 Appendix D
32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 868