我有一个异常类:
#ifndef OBJECTEXCEPTION_H_
#define OBJECTEXCEPTION_H_
class ObjectException: public std::logic_error
{
public:
ObjectException (const std::string& raison)
:std::logic_error(raison){};
};
class Object1Exception: public ObjectException
{
public:
Object1Exception (const std::string& raison)
: ObjectException(raison){};
};
#endif
我有一个抛出此异常的方法:
void Object1::myMethod(int type) {
if (type == 0) {
throw new Object1Exception(type);
}
...
}
现在,我使用这种方法:
try{
obj1->myMethod(0);
}
catch(Object1Exception& error){
}
但是我有这个错误
terminate called after throwing an instance of 'tp::Object1Exception*'
我不明白为什么未捕获到异常。
代码throw Object1Exception(type);
不带new
; 您将抛出一个指向异常的指针,而不是异常本身。
BTW,如评论polkadotcadaver,该错误信息是很清楚的,它告诉你抛出一些指针类型的实例throwing an instance of 'tp::Object1Exception*'
....
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句