Deriving from std::except
I'm deriving from std::except to create my own exception handling classes that the application will use. unit_error below defines an error context much like std::logic_error does, for instance. However, unit_error (and order_error, output_error, army_error...) classes will focus on the object instead of the context surrounding its usage.
Code:
class unit_error: public std::exception {
public:
unit_error(const std::string& err, const unsigned int n): err_(err), errno_(n) {}
virtual ~unit_error() throw() {}
const char* what() const throw() { return err_.c_str(); }
unsigned int error() const throw() { return errno_; }
private:
std::string err_; // Holds error description
unsigned int errno_; // Holds error code
};
Three things are worrying me:
First, does basic_string::c_str() throw? The compiler can't warn me about this and Lint says it does. However, digging in the library code (something I'm not very good at) I can't find any evidence it does. I guess I could program defensively and just do a try-catch inside what() and forget about it, but i'm curious anyway.
Second, error codes are provided in the namespace "murk::error_codes" declared on the same header as these classes. These are const uint variables. However I wonder if there would be a better place to move them. More specificaly inside the exception handling classes as static const members? This way, each class could declare their own related errors and leave the others out.
std::exception is not an abstract class. I can't see any problems with that since I can't imagine a plausible reason for users to want to assign mix-type error handling objects inside a catch clause. However, more often than not I cannot see past my nose, so is there a reason for concern? And what should I do about it? I'm stuck in finding a way if I have to deal with it. It is also possible other classes may derive from unit_error in the future.