2

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.

1
  • 3
    Never say new in C++ unless you're an expert. Commented Dec 8, 2013 at 17:31

1 Answer 1

3

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*'....

Sign up to request clarification or add additional context in comments.

1 Comment

I just want to point out also that the error message told you exactly this - "tp::Object1Exception*", the * meaning pointer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.