使用throw()
声明用户定义的异常的所有成员函数是一种好习惯吗? e.g。
class MyCustomException : public std::exception
{
public:
MyCustomException(const std::string& message, const std::string& additional) :
message_(message), additional_(additional) {}
virtual const char* what() const throw() { return message_.c_str(); }
const char* additional() const throw() { return additional_.c_str(); }
virtual ~MyCustomException() throw() {};
private:
const std::string message_;
const std::string additional_;
};
此处additional()
信息阅读器也将声明为throw()
,这意味着该函数不得抛出异常。