c++ - std::exception 子类,字符串成员变量

标签 c++ string exception

下面的代码工作得很好:

#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 的类型更改为字符串时,就会出现以下编译错误:

Exception specification of overriding function is more lax than base version

最佳答案

std::string的析构函数not不抛出,导致FileException的隐式析构函数not 也不会抛出。但是 std::exception 的析构函数是 no-throw,因此存在编译错误。

您可以声明一个显式的不抛出析构函数:

virtual ~FileException() throw() {}

或者只是继承自 std::runtime_error 而不是 std::exception,它有一个接受 std::string 输入的构造函数.

关于c++ - std::exception 子类,字符串成员变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8618060/

相关文章:

java - 我可以改进这个 Pig-Latin 转换器吗?

python - Python 中是否有 raise foo, bar 的用例?

java - 致命异常 : Main java. lang.RuntimeException:

c# - C#判断字符串是否为数字

c# - 调试时不要中断

c++ - std::vector 中的重复元素

c++ - Qt 中的动态用户界面

c++自定义字符串格式使用stringstreams

c++ - log10函数在cmath中的时间复杂度是多少?

java - 在 java 字符串中转义 % 符号以应用 String.format