Q:

Exception class

So i'm doing a class Exception for linked list, but i have no idea what to put it in. For example, if the user wants to delete an element that does not exist in the list, it will print out "Sparse Exception caught: Element not found, delete fail", or "Sparse Exception caught: sparse matrix empty, printing failed". Is it ok for me to do it this way? If so, how do i print out the message?
SparseException::SparseException ()
{
}
SparseException::SparseException (char *message)
{
if (strcmp(message, "delete"))
      cout << "Sparse Exception caught: Element not found, delete fail";
   else
      cout << "Sparse Exception caught: sparse matrix empty, printing failed";
}

void SparseException::printMessage () const
{

}

A:

In my opinion an exception should always inherit std::exception or one of its subclasses. This will give you an idea of the interface you should implement. You need a what methods for instance(in fact what returns what probably your printMessage would print). Also if you really want to print your exception's what message, better overload the operator<<.
Inheriting std::exception is needed so that you know a catch(std::exception& ex) will catch any exception that your code threw.

