Link to home
Start Free TrialLog in
Avatar of ispcorp
ispcorp

asked on

Error "throws different exception than previous declaration"

Hello,
I'm getting this error in several different places, but here is a copy of one of the error lines...

C:\Project1\studentlist.cpp declaration of `void StudentList::remove(int)'  throws different exceptions C:\Project1\studentlist.h than previous declaration `void  StudentList::remove(int) throw (ListIndexOutOfRangeException)'

What does this mean?  Thanks, and if you need me to post up some code, please don't hesitate to ask....

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ispcorp
ispcorp

ASKER

DECLARATION CODE:
#include "ListException.h"
#include "ListIndexOutOfRangeException.h"
#include "student.h"
#include <iostream>
#include <list>

typedef student ItemType;
const int MAX=15;
class StudentList
{
      public:
            StudentList();
            ~StudentList();
            bool isEmpty() const;
            int getLength() const;
            void insert(int index,ItemType newItem)
                 throw(ListIndexOutOfRangeException);
        void remove(int index)
             throw(ListIndexOutOfRangeException);
        void retreive(int index, ItemType& dataItem) const
             throw(ListIndexOutOfRangeException);
      private:
            struct Node
            {
                   ItemType item;
                   Node *next;
            };
            int size;
            Node *find(int index) const;
            Node *head;
};

 
Avatar of ispcorp

ASKER

IMPLEMENTATION CODE:
#include "studentlist.h" // class's header file
#include <cstddef>
#include <cassert>

StudentList::StudentList(): size(0),head(NULL)
{}
StudentList::~StudentList()
{}
bool StudentList::isEmpty() const
{
     return size == 0;
}
int StudentList::getLength() const
{
    return size;
}
StudentList::Node *StudentList::find(int index) const
{
    if ((index < 1) || (index > getLength()))
       return NULL;
    else
    {
        Node *cur = head;
        for (int skip = 1; skip < index; ++skip)
            cur = cur->next;
        return cur;
    }
}
void StudentList::retreive(int index,ItemType& dataItem) const
{
     if ((index < 1) || (index > getLength()))
        throw ListIndexOutOfRangeException("ListOutOfRangeException: retrieve index out of range");
     else
     {
         Node *cur = find(index);
         dataItem = cur->item;
     }
}
void StudentList::insert(int index,ItemType newItem)
{
     int newLength = getLength() + 1;
     if ((index < 1) || (index > newLength))
        throw ListIndexOutOfRangeException("ListOutRangeException: insert index out of range");
     else
     {
         Node *newPtr = new Node;
         if (newPtr == NULL)
            throw ListException("ListException: insert cannot allocate memory");
         else if(size==MAX)
              throw ListException("ListException: maximum number of students have been reached");
         else
         {
             size = newLength;
             newPtr->item=newItem;
             if (index==1)
             {
                newPtr->next=head;
                head=newPtr;
             }
             else
             {
                 Node *prev = find(index-1);
                 newPtr->next=prev->next;
                 prev->next=newPtr;
             }
         }
     }
}
void StudentList::remove(int index)
{
     Node *cur;
     if ((index < 1) || (index > getLength()))
        throw ListIndexOutOfRangeException("ListOutRangeException: remove index out of range");
     else
     {
         if (index ==1)
         {
            cur = head;
            head = head->next;
         }
         else
         {
             Node *prev = find(index-1);
             cur = prev->next;
             prev->next = cur->next;
         }
         cur->next=NULL;
         delete cur;
         cur=NULL;
     }
}
Hmm, I just see that 'insert()' throws an exception of kind "ListException" that was not declared, but 'remove()' seems OK.
Avatar of ispcorp

ASKER

Excellent, thanks for the example of how to throw an exception with a method.  I should have known, I'm a java programmer and I know you have to put throw after the method name.  But I'm learning C++, and its the little differences that drive me crazy.