?
Solved

Error "throws different exception than previous declaration"

Posted on 2005-03-20
5
Medium Priority
?
1,129 Views
Last Modified: 2008-01-09
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
Comment
Question by:ispcorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13588145
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13588167
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
 
LVL 2

Author Comment

by:ispcorp
ID: 13588169
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
 
LVL 86

Expert Comment

by:jkr
ID: 13588183
Hmm, I just see that 'insert()' throws an exception of kind "ListException" that was not declared, but 'remove()' seems OK.
0
 
LVL 2

Author Comment

by:ispcorp
ID: 13588185
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question