home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / EXCEPT.CPP < prev    next >
C/C++ Source or Header  |  1993-03-20  |  4KB  |  99 lines

  1. /******************************************************************************/
  2. /* SAMPLE PROGRAM                                                             */
  3. /*                                                                            */
  4. /* COPYRIGHT:                                                                 */
  5. /* ----------                                                                 */
  6. /* Copyright (C) International Business Machines Corp., 1991,1992.            */
  7. /*                                                                            */
  8. /* DISCLAIMER OF WARRANTIES:                                                  */
  9. /* -------------------------                                                  */
  10. /* The following [enclosed] code is sample code created by IBM                */
  11. /* Corporation.  This sample code is not part of any standard IBM product     */
  12. /* and is provided to you solely for the purpose of assisting you in the      */
  13. /* development of your applications.  The code is provided "AS IS",           */
  14. /* without warranty of any kind.  IBM shall not be liable for any damages     */
  15. /* arising out of your use of the sample code, even if they have been         */
  16. /* advised of the possibility of such damages.                                */
  17. /*                                                                            */
  18. /******************************************************************************/
  19. /*                                                                            */
  20. /*  Sample to demonstrate exception handling                                  */
  21. /*                                                                            */
  22. /*    This example demonstrates a vector class that throws an exception       */
  23. /*    to check for upper bound exceeded.                                      */
  24. /*                                                                            */
  25. /*    A vector class is created which contains a constructor and an           */
  26. /*    overloaded [] operator.  The constructor stores the size of the         */
  27. /*    vector when it is instantiated (sz) and uses that size in the []        */
  28. /*    operator to check for bounds exceeded.                                  */
  29. /*                                                                            */
  30. /*    If the user tries to access element greater than the instantiated       */
  31. /*    size, an exception will be thrown.  The user is responsible for         */
  32. /*    catching the out of bound exception.                                    */
  33. /*                                                                            */
  34. /******************************************************************************/
  35.  
  36. #include <iostream.h>
  37. #include <terminat.h>
  38.  
  39.  
  40. typedef void (*PFV)();
  41. void my_terminate()
  42. {
  43.    cout << "FATAL ERROR: Please contact you service representative\n";
  44. }
  45.  
  46.  
  47. class Vector {
  48.     int* p;
  49.     int sz;
  50.   public:
  51.     enum { max = 32000 };
  52.     class Range {             // Range exception to be thrown
  53.       public:
  54.         int index;
  55.         Range(int i) : index(i) { }
  56.     };
  57.  
  58.     Vector(int s);              // constructor
  59.     int& operator [] (int i);   // operator
  60.     ~Vector() { delete [] p; }  // destructor clean up storage
  61. };
  62.  
  63. Vector::Vector(int size) {
  64.    p = new int[sz=size];               // save initial size of vector in sz
  65. }
  66.  
  67. int& Vector::operator[] (int i) {
  68.    if (0<=i && i<sz) return p[i];      // check bounds
  69.    throw Range(i);                     // throw exception
  70. }
  71.  
  72.  
  73. void main(void)
  74. {
  75.  
  76.   // set the terminate function to catch unexpected exceptions
  77.   PFV oldterm = set_terminate(my_terminate);
  78.  
  79.   // try block using the vector class
  80.   try {
  81.       Vector v(10);
  82.  
  83.       for (int i=0; i<10; i++) v[i] = i;
  84.  
  85.       for (i=0; i<10; i++) cout << v[i] << " ";
  86.  
  87.       cout << v[20] << endl;
  88.   }
  89.   // catch block for out of range errors
  90.   catch (Vector::Range x) {
  91.      cout << "Vector::Range error: invalid index:" << x.index << endl;
  92.   }
  93.   // match for any exception not caught above
  94.   catch (...) {
  95.      cout << "Catch other exceptions\n";
  96.   }
  97.  
  98. }
  99.