Im attempting to throw an EmptyListException when my linked list is empty, but the program keeps terminating if i uncomment the throw EmptyListException().
this is my EmptyListException header
#ifndef EMPTYLISTEXCEPTION_H
#define EMPTYLISTEXCEPTION_H
#include <stdexcept>
using std::out_of_range;
class EmptyListException : public out_of_range
{
public:
EmptyListException(): out_of_range("Empty List!\n") {}
};
#endif // EMPTYLISTEXCEPTION_H
— Throw command in Clist.h
template <typename E>
E Clist<E>::Remove() throw()
{
if(isEmpty())
{
cout << "Empty List, no removal";
//throw EmptyListException();
return '.';
}
... code
}
— catch in Main
try{
cout << list->Remove() << endl;
} catch(EmptyListException &emptyList)
{
cout << "Caught :";
cout << emptyList.what() << endl;
}
The error ‘This application has requested the Runtime to terminate it in an unusual way. Please contact the application’s support team for more information.
Well, you told the compiler that you will not throw any exceptions from your
Remove()! When you violate this promise it terminates the program. Get rid ofthrow()in the function declaration and try again.