mardi 17 mars 2015

C++ Exception Handling between Classes


I'm working on exception handling and right now I'm stuck on a problem I'm working on out of my book. What I'm trying to do is add an try-catch block inside my main function to catch an exception that is thrown by a function that I am using. Here are snippets of where I am having trouble on.


EmptyStackPopException is an empty Exception class that I put into my stack.h file that can be seen here:



class EmptyStackPopException {
};


Here is the function that is supposed to throw this Exception.



T Stack<T>::pop( ) throw (EmptyStackPopException)
{
if (isEmpty( ))
{
throw EmptyStackPopException();
}

T result = top->getData( );

Node<T> *discard;
discard = top;
top = top->getLink( );

delete discard;

return result;
}


Here is a snippet of my main function where the exception is supposed to be caught:



try {
cout << s.pop( );
} catch (EmptyStackPopException) {
cout << "EmptyStackPopException: You didn't enter anything in." << endl;
}


Now the issue that I'm having is I cannot compile because of these following errors:



..\assignment8\main.cpp(29) : error C2061: syntax error : identifier 'EmptyStackPopException'
..\assignment8\main.cpp(29) : error C2310: catch handlers must specify one type
..\assignment8\main.cpp(32) : error C2317: 'try' block starting on line '27' has no catch handlers


I'm pretty sure that the only error I have at the moment is in the main function, EmptyStackPopException is not able to be detected in the client program which causes the last two errors.




Aucun commentaire:

Enregistrer un commentaire