Reputation: 34
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
{
}
Upvotes: 0
Views: 215
Reputation: 10222
If you want to create a custom Exception
, you'd better inherit it from std::exception
or its subtypes like runtime_error
or logic_error
. Besides, it makes no sense to print message in the exception class, that's client's responsibility. You only need override what
method, which returns detailed exception message(that said, runtime_error
or logic_error
's default implementation is usually OK).
Statement if (strcmp(message, "delete")) {...}
makes little sense, too(actually it should be !strcmp(...)
). If you just want to print different message, passing that message to the exception's constructor is enough. However, if you need distinguish exceptions, you'd better add some class like SparseDeletionException
or SparseEmptyException
that inherits from common base SparseException
(which in turn inherits from std::exception
).
Upvotes: 0
Reputation: 71009
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.
Upvotes: 4
Reputation: 27385
So i'm doing a class Exception for linked list, but i have no idea what to put it in.
First, consider inheriting from std::exception hierarchy (usually I inherit from std::runtime_error or std::logic_error). It will provide you with a basic class receiving a string message that describes the error and allow your client code to receive and handle your exceptions by catching a generic std::exception ref.
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?
Normally you should use a complete message (a string that can be logged) that describes the error, but not the type of exception (leave that to your exception classes hierarchy).
in your case:
class SparseException: public std::runtime_error
{
public:
SparseException(const std::string& message): std::runtime_error(message) {}
};
Your queue:
// ...
if (whatever1)
throw SparseException("element not found, delete failed");
// ...
if (whatever2)
throw SparseException("sparse matrix empty, printing failed");
If so, how do i print out the message?
Client code:
try
{
// ...
} catch(const SparseException& e)
{
std::cerr << "SparseException: " << e.what() << "\n"; // what() is a member
// of std::exception
}
Upvotes: 0
Reputation: 11833
Usually, exception classes simply contain information about what went wrong. Usually, they contain a string, because strings can contains about just everything you want "Writing 123 to file c:/a.txt failed".
In the simple case you present, you should rather use different classes of Exceptions (like a SparseDeleteException). Or, if you want to have more complex logic to get error texts, I would suggest to use a different class to handle "error id" -> "error string" conversions, because this is not the task of the exception.
Furthermore, the exception should have a method toString
instead of print - the catching method should be able to decide what to do with the message.
Upvotes: 0
Reputation: 60017
Adding to @Ivaylo post, the line
if (strcmp(message, "delete"))
should read
if (0 == strcmp(message, "delete"))
Upvotes: 0