home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX1603.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-24  |  2.4 KB  |  93 lines

  1. // \EXAMPLES\EX1603.CPP
  2.  
  3. //----------------------------------------------------------
  4. //  Exception handling is supported only by
  5. //                            the IBM C Set++ Compiler
  6. //  This example does not work in Borland Turbo C++
  7. //                          or in Microsoft Visual C++
  8. //----------------------------------------------------------
  9. //  To run this program on an OS/2 machine:
  10. //                      move to an OS/2 window
  11. //              [type]  cd:\EXAMPLES\EX1603I
  12. //                      where cd: is the drive of the CD-ROM
  13. //----------------------------------------------------------
  14.  
  15. #include <iostream.h>
  16.  
  17. //
  18. // This is example shows the various forms of catch declarations
  19. //
  20. void Process( int i)
  21. {
  22.    try
  23.    {
  24.       if (i == 1)
  25.       {
  26.          throw i;                    // throw an integer
  27.       }
  28.       else if (i ==2)
  29.       {
  30.          throw (float)i;             // throw a float
  31.       }
  32.       else if (i == 3)
  33.       {
  34.          throw double(i);            // throw a double
  35.       }
  36.    }
  37.    catch( int y)                     // catching an expression
  38.    {
  39.       if (y >= 0)
  40.       {
  41.          cout << "Exception handler: catch( int y){ ...}"
  42.               << endl
  43.               << "A integer exception greater than zero caught,"
  44.               << " its value is: "
  45.               << y << endl << endl;
  46.       }
  47.       else
  48.       {
  49.          cout << "A integer exception less than zero was caught,"
  50.               << " its value is: "
  51.               << y << endl;
  52.       }
  53.    }
  54.    catch( float)                     // catching a type
  55.    {
  56.       cout << "Exception handler: catch( float){ ...}" << endl
  57.            << "A floating point exception was caught."
  58.            << "  Its value is unknown "
  59.  
  60.  
  61.            << endl << endl;
  62.       throw;
  63.    }
  64.    catch(...)           // catching an exception of unknown type
  65.    {
  66.       cout << "Exception handler: catch( ...){ ...}" << endl
  67.            << "An exception was caught, "
  68.            << "but there is no way of knowing the type. "
  69.            << endl << endl;
  70.       throw;
  71.    }
  72. }
  73.  
  74.  
  75. void main()
  76. {
  77.   //
  78.   // Each iteration of the loop causes the invocation
  79.   // of an exception handler
  80.   // using a different catch declaration
  81.   //
  82.   for (int i = 1; i <= 3; i++) {
  83.      try {
  84.         Process( i);
  85.      }
  86.      catch (...)
  87.      {
  88.         cout << "The rethrown object was caught. "
  89.              << endl << endl;
  90.      }
  91.   }
  92. }
  93.