Reputation: 10089
I have an exception class:
#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
I have a method which throw this exception:
void Object1::myMethod(int type) {
if (type == 0) {
throw new Object1Exception(type);
}
...
}
Now I use this method:
try{
obj1->myMethod(0);
}
catch(Object1Exception& error){
}
But I have this error
terminate called after throwing an instance of 'tp::Object1Exception*'
I don't understand why the exception is not caught.
Upvotes: 2
Views: 557
Reputation: 1
Code throw Object1Exception(type);
without the new
; you are throwing a pointer to an exception, not an exception itself.
BTW, as commented by polkadotcadaver, the error message was pretty clear, it told you about throwing an instance of some pointer type throwing an instance of 'tp::Object1Exception*'
....
Upvotes: 3