home *** CD-ROM | disk | FTP | other *** search
/ Using Visual C++ 4 (Special Edition) / Using_Visual_C_4_Special_Edition_QUE_1996.iso / ch13 / unwind.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  644 b   |  31 lines

  1. // Get needed include files
  2. #include <limits.h>
  3. #include <iostream.h>
  4. #include <eh.h>
  5.  
  6. class MyMemoryHogClass {
  7. public:
  8.     ~MyMemoryHogClass() { cout << "In the MyMemoryHogClass destructor.\n"; }
  9. };
  10.  
  11. unsigned short Add(unsigned short addend1, unsigned short addend2)
  12. {
  13.     MyMemoryHogClass Hog;
  14.     unsigned long sum = addend1 + addend2;
  15.     if (sum > USHRT_MAX)
  16.         throw 1;
  17.     return (unsigned short) sum;
  18. }
  19.  
  20. void main()
  21. {
  22.     try {
  23.         unsigned short Result = Add(12345, 54321);
  24.         cout << "The answer is " << Result << "\n";
  25.     }
  26.     catch (int ErrorCode) {
  27.         cout << "An overflow occurred! ErrorCode = "
  28.              << ErrorCode << "\n";
  29.     }
  30. }
  31.