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

  1. // \EXAMPLES\EX1607.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\EX1607I
  12. //                      where cd: is the drive of the CD-ROM
  13. //----------------------------------------------------------
  14.  
  15. #include <iostream.h>
  16. #include <string.h>
  17. #include <stdio.h>
  18.  
  19. //
  20. // This example shows how to use inheritance to organize
  21. // exceptions into a class hierarchy
  22. //
  23. typedef char String32[33];
  24.  
  25. //
  26. // Exception is the base exception class
  27. //
  28. class Exception {
  29.  
  30. protected:
  31.    String32 sName;
  32.  
  33. public:
  34.    //
  35.    // Constructor, sets up the name of the exception
  36.    //
  37.    Exception() { strcpy( (char *)sName, "Exception");}
  38.  
  39.    //
  40.    // Name returns the name of the exception
  41.    //
  42.    char*
  43.    Name(){return (char *)sName;}
  44. };
  45.  
  46. //
  47. // MathExcept is derived from exception
  48. //            use to throw math exception
  49. //
  50. class MathExcept : public Exception
  51. {
  52.  
  53. public:
  54.    //
  55.    // Constructor, sets up the name of the exception
  56.    //
  57.    MathExcept(){ strcpy( (char *)sName, "Math Exception"); }
  58. };
  59.  
  60.  
  61. //
  62. // IOExcept, is derived from class Exception
  63. //           use to throw ioexcept exceptions
  64. //
  65. class IOExcept : public Exception
  66. {
  67. public:
  68.    //
  69.    // Constructor, sets up the name of the exception
  70.    //
  71.    IOExcept(){ strcpy( (char *)sName, "IO Exception"); }
  72. };
  73.  
  74. //
  75. // BoundsExcept, is derived from class Exception
  76. //           use to throw a Bounds Violation exceptions
  77. //
  78. class BoundsExcept : public Exception
  79. {
  80. public:
  81.    //
  82.    // Constructor, sets up the name of the exception
  83.    //
  84.    BoundsExcept(){ strcpy( (char *)sName, "Bounds Exception"); }
  85. };
  86.  
  87.  
  88. //
  89. // divide: returns x divided by y
  90. //         throws MathExcept if y == 0
  91. //
  92. long divide( long x, long y)
  93. {
  94.    if (y==0) {
  95.       throw( MathExcept());
  96.    }
  97.    return (x/y);
  98. }
  99.  
  100. //
  101. // read: dummy function
  102. //       throws IOExcept
  103. //
  104. void read()
  105. {
  106.    FILE *fp;
  107.  
  108.    if ( fp = fopen( "Egon", "r"), fp == NULL) {
  109.       throw( IOExcept());
  110.    }
  111.    // read a file
  112. }
  113.  
  114. //
  115. // access returns an element of a char array
  116. //                      throw Exception
  117. //
  118. char access( unsigned int index)
  119. {
  120.    char array[] = {'a', 'b', 'c'};
  121.    if (index >= 3) {
  122.          throw( BoundsExcept());
  123.    }
  124.    else
  125.       return array[index];
  126. }
  127.  
  128. //
  129. // The main routine calls three functions, each of which calls
  130. // an exception
  131. //
  132. main()
  133. {
  134.    for (int i = 1; i <= 3 ; i++)
  135.    {
  136.       try
  137.       {
  138.          if ( i == 1 )
  139.             divide(1,0);        // attempt to divide 1 by 0
  140.          else if (i == 2)
  141.             read();             // always throws an IOExcept
  142.          else if (i == 3)
  143.             access( 4);         // always throws an Exception
  144.       }
  145.       catch ( MathExcept except)
  146.       {
  147.          cout << "A(n) " << except.Name()
  148.            << " exception was caught by the MathExcept handler."
  149.            << endl;
  150.       }
  151.       catch ( IOExcept except)
  152.       {
  153.          cout << "A(n) " << except.Name()
  154.             << " exception was caught by the IOExcept handler."
  155.             << endl;
  156.       }
  157.       catch ( Exception except)
  158.       {
  159.          cout << "A(n) " << except.Name()
  160.             << " exception was caught by the Exception handler."
  161.             << endl;
  162.       }
  163.    }
  164. }
  165.