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

  1. // \EXAMPLES\EX1605.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\EX1605I
  12. //                      where cd: is the drive of the CD-ROM
  13. //----------------------------------------------------------
  14.  
  15. #include <iostream.h>
  16.  
  17. //
  18. // This example shows how to rethrow an exception
  19. //
  20.  
  21. void Process();
  22. long divide(long, long);
  23.  
  24. main()
  25. {
  26.    //
  27.    // Try block, catches exception thrown in Process' call chain
  28.    //
  29.    try
  30.    {
  31.       Process();            // call the function to do the work
  32.    }
  33.    catch( long z)           // catch the floating point exception
  34.    {
  35.       cout << "A long exception was caught, "
  36.            << "its value was: " << z << endl;
  37.    }
  38. }
  39.  
  40. //
  41. // FUNCTION: Process(...) does the work
  42. //
  43. void Process()
  44. {
  45.    try
  46.    {
  47.       int x, y;
  48.       cout << "Enter two numbers:  ";
  49.       cin >> x >> y;              // read in two numbers
  50.       cout << endl << x << " divided by " << y << " is: "
  51.            << divide(x, y);       // print the quotient
  52.    }
  53.    catch( long y)                 // catch the exception
  54.    {
  55.        cout << "A long exception was caught and re-thrown"
  56.             << endl;
  57.        throw;                      // rethrow
  58.    }
  59. }
  60.  
  61. //
  62. // FUNCTION: divide(...) divides first parameter by second
  63. // PARAMETERS: long x dividend
  64. //             long y divisor
  65. // RETURN VALUE: x divided by y
  66. //
  67. long divide(long x, long y)
  68. {
  69.    if (y==0)
  70.    {
  71.       throw y;     // can't divide by zero so throw an exception
  72.    }
  73.    return x/y;     // return the quotient
  74. }
  75.