【发布时间】:2014-06-10 08:42:55
【问题描述】:
我试图弄清楚如何为允许添加错误特定文本字符串的 C++ 异常实现我自己的基类。我认为 std::c++ 异常不可能更改错误文本,即使在 C++11 中也不可能:http://www.cplusplus.com/reference/exception/exception/。 我还想从我的 Exception 基类中派生出更具体的异常。我阅读了很多文章,但我仍然不确定我的以下实现是否涵盖了所有重要方面。
class Exception : public std::exception {
public:
Exception(const char* message) : m(message) {
}
virtual ~Exception() throw() {
}
virtual const char* what() const throw() {
return m.c_str();
}
protected:
std::string m;
private:
Exception();
};
这个实现好吗?
【问题讨论】:
-
你看到了什么问题?应该张贴在codereview.stackexchange.com
-
请注意,您也可以将
std::string提供给std::runtime_error。