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

  1. // \EXAMPLES\EX1613.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\EX1613I
  12. //                      where cd: is the drive of the CD-ROM
  13. //----------------------------------------------------------
  14.  
  15. #include <iostream.h>
  16. #include <unexpect.h>
  17.  
  18. //
  19. // This is example illustrates the use of set_unexpected
  20. //
  21. typedef void (*PFV)(); // Pointer to function that returns void
  22. PFV  pfvOldUnexpected; // use to store old unexpected function.
  23.  
  24. //
  25. // FUNCTION: divide(...) divides first parameter by the second
  26. // PARAMETERS: long x dividend
  27. //             long y divisor
  28. // RETURN VALUE: x divided by y
  29. //
  30. long divide(long x, long y) throw( long)
  31. {
  32.    if (y==0)
  33.    {
  34.       throw y;   // can't divide by zero so throw an exception
  35.    }
  36.    return x/y;   // return the quotient
  37. }
  38.  
  39.  
  40. void Process() throw()
  41. {
  42.    try
  43.    {
  44.       divide( 1, 0);
  45.    }
  46.    catch( char)   // char exception handler won't catch a float
  47.    {
  48.       cout << "This line should not be printed." << endl;
  49.    }
  50. }
  51.  
  52. //
  53. // This is the user defined unexpected exception handler
  54. //
  55. void myUnexpected()
  56. {
  57.    cout << "Invoked myUnexpected()." << endl;
  58.  
  59.    set_unexpected( (PFV)pfvOldUnexpected);
  60.    cout << "Reset unexpected function." << endl;
  61.  
  62.    // Insert code to free resources here
  63.  
  64.    cout << "Calling Process() from myUnexpected()" << endl;
  65.    Process();
  66. }
  67.  
  68.  
  69. main()
  70. {
  71.    // set the unexpected function to myUnexpected
  72.    pfvOldUnexpected = set_unexpected( (PFV)myUnexpected);
  73.  
  74.    cout << "Set the unexpected function to myUnexpected()."
  75.         << endl;
  76.  
  77.    Process();          // Call the function to do the work.
  78. }
  79.