异常继承
Exception inheritance
是否可能在c++的异常类中有多个元素,我可以将它们与异常关联起来,这样当我抛出它时,用户可以收集更多关于异常的信息,而不仅仅是错误消息?我有下面的类
#include <list>
using namespace std;
class myex : public out_of_range {
private:
list<int> *li;
const char* str = "";
public:
//myex(const char* err): out_of_range(err) {}
myex(li<int> *l,const char* s) : li(l),str(s) {}
const char* what(){
return str;
}
};
当我使用
抛出myex时throw myexception<int>(0,cont,"Invalid dereferencing: The iterator index is out of range.");,
我得到一个错误
error: no matching function for call to ‘std::out_of_range::out_of_range()’.
Any help is appreciated.`.
当我取消注释的行,并删除另一个构造函数,然后它工作正常
用户定义的异常的构造函数试图调用out_of_range类的默认构造函数…除非它不存在!
关于注释的构造函数:
myex(const char* err): out_of_range(err) {}
//^^^^^^^^^^^^^^^^^ this calls the constructor of
// out_of_range with the parameter err.
为了修复当前的构造函数,应该添加对out_of_range的构造函数的显式调用(该构造函数接受const string&):
myex(li<int> *l,const char* s) : out_of_range(s), li(l),str(s) {}
相关文章:
- C++中模板化异常类的多重继承
- 自定义异常中的用户定义的空构造函数,具有多个继承和抽象基类
- 关于异常继承中的语法的问题
- 无法创建两个从 std::logic_error 继承的自定义异常类
- 如何在继承的类中没有构造函数时引发异常
- 异常类的多重继承
- C++通过继承 std::exception 来创建新的异常
- 创建继承自C++中io类的异常类
- 模板多重继承异常符号错误
- 为什么异常捕获是基于顺序的,而不是基于最接近继承的
- C++异常继承模糊性
- 从boost::exception和std::runtime_error继承自定义异常类
- C++ 继承另一个库中的库异常,以将它们传递给调用代码
- 使用继承和运算符重载或异常处理的通讯簿
- C++和并发编程中的继承异常
- c++中异常类的继承
- 异常继承
- 你能把一个异常继承层次结构包装到另一个吗?——或者,另一种干净的处理方式
- 异常是否需要虚拟继承?
- 为什么继承影响异常处理