【发布时间】:2021-10-31 23:48:19
【问题描述】:
以下代码可以正常工作:
#include <exception>
using namespace std;
class FileException : public exception { // error occurs here
int _error;
// string _error; <-- this would cause the error
public:
FileException(int error);
// FileException(string error);
const char* what() const throw();
};
但是一旦我将_error的类型更改为字符串,就会出现以下编译错误:
覆盖函数的异常规范比基础版本更宽松
【问题讨论】:
-
从 std::runtime_error 派生。您可以将一个字符串传递给它的构造函数,它将存储它以使用 what() 进行重试。因此,您不需要管理字符串。