使用场景
// string exception class, where the exception is described by a std::stringclass Exception : public std::exception{public:Exception(const std::string& err) noexcept : err_(err) {}Exception(std::string&& err) noexcept : err_(std::move(err)) {}virtual const char* what() const throw() { return err_.c_str(); }const std::string& err() const noexcept { return err_; }virtual ~Exception() throw() {}void add_label(const std::string& label){err_ = label + ": " + err_;}void remove_label(const std::string& label){const std::string head = label + ": ";if (string::starts_with(err_, head))err_ = err_.substr(head.length());}private:std::string err_;};
可以看到Exception类的成员有的用noexcept表示该成员函数不会抛出异常。
有的用的是throw()表示不会抛出异常。
throw(…) 表示会抛异常,指定参数的话,就表示只会抛某类异常,通常不建议使用。
参考
https://stackoverflow.com/questions/12833241/difference-between-c03-throw-specifier-c11-noexcept
https://stackoverflow.com/questions/88573/should-i-use-an-exception-specifier-in-c/88905#88905
