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

  1. // \EXAMPLES\EX1610.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\EX1610I
  12. //                      where cd: is the drive of the CD-ROM
  13. //----------------------------------------------------------
  14.  
  15. #include <iostream.h>
  16. #include <string.h>
  17.  
  18. //
  19. // This is example shows the use of exception specifications
  20. //
  21.  
  22. typedef char String32[33];
  23.  
  24. class Exception {
  25.  
  26. protected:
  27.    String32 sName;
  28.  
  29. public:
  30.    Exception() { strcpy( (char *)sName, "Exception");}
  31.  
  32.    char*
  33.    Name(){return (char *)sName;}
  34. };
  35.  
  36. class MathExcept : public Exception
  37. {
  38.  
  39. public:
  40.    MathExcept(){ strcpy( (char *)sName, "Math Exception"); }
  41. };
  42.  
  43.  
  44. long divide( long x, long y) throw( Exception)
  45. {
  46.    if (y==0)
  47.    {
  48.       cout << "Exception Specification: throw( Exception);"
  49.            << endl
  50.            << "Throwing a MathExcept " << endl;
  51.       throw ( MathExcept());
  52.    }
  53.    return (x/y);
  54. }
  55.  
  56. main()
  57. {
  58.    try
  59.    {
  60.       long z = divide(1, 0);
  61.    }
  62.    catch( Exception except)
  63.    {
  64.       cout << "A(n) " << except.Name()
  65.            << " exception was caught."
  66.            << endl;
  67.    }
  68. }
  69.