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

  1. // \EXAMPLES\EX1604.CPP
  2. //
  3. // A complete example of exception handling
  4. //
  5. //----------------------------------------------------------
  6. //  Exception handling is supported only by
  7. //                            the IBM C Set++ Compiler
  8. //  This example does not work in Borland Turbo C++
  9. //                          or in Microsoft Visual C++
  10. //----------------------------------------------------------
  11. //  To run this program on an OS/2 machine:
  12. //                      move to an OS/2 window
  13. //              [type]  cd:\EXAMPLES\EX1604I
  14. //                      where cd: is the drive of the CD-ROM
  15. //----------------------------------------------------------
  16.  
  17. #include <iostream.h>
  18.  
  19. //
  20. // FUNCTION: divide(...) divides first parameter by second
  21. // PARAMETERS: long x dividend
  22. //             long y divisor
  23. // RETURN VALUE: x divided by y
  24. //
  25. long divide( long x, long y)
  26.  
  27. {
  28.    if (y==0) {
  29.       throw( y);
  30.    } else {
  31.       return(x/y);
  32.    }
  33. }
  34.  
  35. //
  36. // FUNCTION: Process(...) does the work
  37. //
  38. void Process()
  39. {
  40.    long z = divide( 1, 0);   // call divide
  41. }
  42.  
  43. void main()
  44. {
  45.    //
  46.    // Try block, catches exception thrown in Process' call chain
  47.    //
  48.    try
  49.    {
  50.       Process();
  51.    }
  52.    catch( long x) {
  53.       cout << "Exception handler: catch( long x)" << endl
  54.            << "The exception value is: " << x << endl;
  55.    }
  56.    catch( ...) {
  57.       cout << "Caught something" << endl;
  58.    }
  59. }
  60.