2
我有一个异常类:捕捉自定义异常C++
#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*'
我不明白为什么没有发现异常。
永远不要说'new'在C++中,除非你专家。 –