home *** CD-ROM | disk | FTP | other *** search
/ Mastering Microsoft Visual C++ 4 (2nd Edition) / VisualC4.ISO / cpp / unwind.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-10-11  |  1.3 KB  |  77 lines

  1. // Unwind.cpp: Example program illustrating the destruction of objects during
  2. //             the unwinding of the stack caused by an exception.
  3.  
  4. #include <iostream.h>
  5.  
  6. class CA
  7. {
  8. public:
  9.    CA ()
  10.       {
  11.       cout << "class CA constructor called" << '\n';
  12.       }
  13.    ~CA ()
  14.       {
  15.       cout << "class CA destructor called" << '\n';
  16.       }
  17. };
  18.  
  19. class CB
  20. {
  21. public:
  22.    CB ()
  23.       {
  24.       cout << "class CB constructor called" << '\n';
  25.       }
  26.    ~CB ()
  27.       {
  28.       cout << "class CB destructor called" << '\n';
  29.       }
  30. };
  31.  
  32. class CC
  33. {
  34. public:
  35.    CC ()
  36.       {
  37.       cout << "class CC constructor called" << '\n';
  38.       }
  39.    ~CC ()
  40.       {
  41.       cout << "class CC destructor called" << '\n';
  42.       }
  43. };
  44.  
  45. CC *PCC = 0;  // define global pointer to class CC
  46.  
  47. void Func ()
  48.    {
  49.    CB B;  // define an instance of class CB
  50.  
  51.    PCC = new CC;  // dynamically create an instance of class CC 
  52.  
  53.    throw "exception message";
  54.  
  55.    delete PCC;
  56.    }
  57.  
  58. void main ()
  59.    {
  60.    cout << "beginning of main ()" << '\n';
  61.    try
  62.       {
  63.       CA A;  // define an instance of class CA
  64.  
  65.       Func ();
  66.  
  67.       cout << "end of try block" << '\n';
  68.       }
  69.    catch (char * ErrorMsg)
  70.       {
  71.       cout << ErrorMsg << '\n';
  72.  
  73.       delete PCC;
  74.       }
  75.    cout << "end of main ()" << '\n';
  76.    }
  77.