title: C++ exception handling and template
body: <p>I'm new to C++ and have this assignment about exception handling and templates that is giving me problems. I can't figure out why my <strong>catch</strong> isn't working. I'm getting "error: expected unqualified-id before ')' token. Can someone correct this problem and explain why I'm getting this error? </p>
  
  <p>Thanks to all who can guide me. </p>
  
  <p>Edit: Following Tony D's comment, the catch and compiler works, but isn't printing the catch message. The exception class <strong>Error</strong> isn't working as I coded. Any hints on how to fix the class?</p>
  
  <p>Here is my program:</p>
  
  <pre><code>// This program demonstrates an overloaded [] operator.
  #include &lt;iostream&gt;
  #include &lt;cstdlib&gt;
  using namespace std;
  
  class IntArray
  {
      private:
      int *aptr;
      int arraySize;
  
  public:
      IntArray(int);       // Constructor
      IntArray(const IntArray &amp;); // Copy constructor
      ~IntArray();         // Destructor
      int size() const { return arraySize; }
      void subError() const;   // Handles subscripts out of range
      int &amp;operator[](int) const; // Overloaded [] operator
  };
  class Error
  {
  public:
  int value;
  Error(int i)
  {
      value=i;
  }
  };
  IntArray::IntArray(int s)
  {
      arraySize = s;
      aptr = new int [s];
      for (int count = 0; count &lt; arraySize; count++)
          *(aptr + count) = 0;
  }
  IntArray::IntArray(const IntArray &amp;obj)
  {
      arraySize = obj.arraySize;
      aptr = new int [arraySize];
      for(int count = 0; count &lt; arraySize; count++)
          *(aptr + count) = *(obj.aptr + count);
  }
  IntArray::~IntArray()
  {
      if (arraySize &gt; 0) 
      {
         delete [] aptr;
         arraySize = 0;
         aptr = NULL;
      }
  }
  void IntArray::subError() const
  {
      cout &lt;&lt; "ERROR: Subscript out of range.\n";
      exit(0);
  }
  int &amp;IntArray::operator[](int sub) const
  {
      if (sub &lt; 0 || sub &gt;= arraySize)
          throw Error(*aptr);
      return aptr[sub];
  }
  int main()
  {
  const int SIZE = 10;  // Array size
  
  // Define an IntArray with 10 elements.
  IntArray table(SIZE);
  try
  {
  // Store values in the array.
      for (int x = 0; x  &lt; SIZE; x++)
      {
              table[x] = (x * 2);
      }
  // Display the values in the array.
      for (int x = 0; x  &lt; SIZE; x++)
      {
          cout &lt;&lt; table[x] &lt;&lt; " ";
  
      }
      cout &lt;&lt; endl;
      // Try to print an element out of bounds
      cout &lt;&lt; table[-1] &lt;&lt; endl;
  }
  catch(const Error&amp;)
  {
      table.subError();
  }
  return 0;
  }
  </code></pre>