home *** CD-ROM | disk | FTP | other *** search
- // Unwind.cpp: Example program illustrating the destruction of objects during
- // the unwinding of the stack caused by an exception.
-
- #include <iostream.h>
-
- class CA
- {
- public:
- CA ()
- {
- cout << "class CA constructor called" << '\n';
- }
- ~CA ()
- {
- cout << "class CA destructor called" << '\n';
- }
- };
-
- class CB
- {
- public:
- CB ()
- {
- cout << "class CB constructor called" << '\n';
- }
- ~CB ()
- {
- cout << "class CB destructor called" << '\n';
- }
- };
-
- class CC
- {
- public:
- CC ()
- {
- cout << "class CC constructor called" << '\n';
- }
- ~CC ()
- {
- cout << "class CC destructor called" << '\n';
- }
- };
-
- CC *PCC = 0; // define global pointer to class CC
-
- void Func ()
- {
- CB B; // define an instance of class CB
-
- PCC = new CC; // dynamically create an instance of class CC
-
- throw "exception message";
-
- delete PCC;
- }
-
- void main ()
- {
- cout << "beginning of main ()" << '\n';
- try
- {
- CA A; // define an instance of class CA
-
- Func ();
-
- cout << "end of try block" << '\n';
- }
- catch (char * ErrorMsg)
- {
- cout << ErrorMsg << '\n';
-
- delete PCC;
- }
- cout << "end of main ()" << '\n';
- }
-