Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1157
  • Last Modified:

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....

0
ispcorp
Asked:
ispcorp
  • 3
  • 2
1 Solution
 
jkrCommented:
Could you post the declarations? From what you posted, you have a member function declared to throw 'ListIndexOutOfRangeException', but a different implementation in 'studentlist.cpp' which apparently doesn't. Try to make both signatures read the same, i.e.

StudentList::remove(int) throw (ListIndexOutOfRangeException)
{

// code
}
0
 
ispcorpAuthor Commented:
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;
};

 
0
 
ispcorpAuthor Commented:
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;
     }
}
0
 
jkrCommented:
Hmm, I just see that 'insert()' throws an exception of kind "ListException" that was not declared, but 'remove()' seems OK.
0
 
ispcorpAuthor Commented:
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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now