"Stephen torri"
On Sun, 2004-08-22 at 23:41, Jonathan Turkanis wrote:
"Stephen torri"
wrote in message news:1093230456.8377.44.camel@base.torri.org... Are there any guidelines on where and when to use exceptions?
Thanks for the link. I had already found this link but was confused on some of advice it gave. The question below are some of my confusion about using exceptions wisely. What I am seeking for each of them is an example explaining how it implements a solution.
1) Use virutal inheritance. I see the example and can understand why its wrong I just do not understand how to fix it.
2) Don't embed a std::string ... How can I use the data I have at
I believe in the example the fix is to use virtual inheritance in the definitions of my_exc1 and my_exc2, so that later someone can derive from both. Using virtual inheritance in the definition of your_exc3 doen't help. I see that this advice is not followed consistently within boost (Robert Ramey's serialization library is the only case I can find). But it's still good advice, I think. the
point of the error safely? I want to provide a debug message to the user that will lead them to the answer. This affects 6) Expose relevant information about the cause of the error.
It's not always possible to satisfy condition (2), but you should do it whenever possible.
In my project I have a base class called BaseException that inherits from std::exception.
class DataTypeException : public BaseException { DataTypeException (std::string msg) : Base_Exception(), m_msg(msg) {}
// Specify virtual functions? virtual const char* what() const throw() { try { std::stringstream output; output << m_exception_name << ": " << m_message.c_str() << std::endl; return (output.str()).c_str(); } catch(std::exception &) { return m_exception_name; } } private:
std::string m_msg; };
Often you can get away with: class Data_Exception : public std::exception { public: Data_Exception(int code) : code_(code) { } virtual const char* what() const throw() { [return result of looking up code in a table of error messages.] } private: int code; }; As I said, this isn't always sufficient, but it often is. Jonathan