NeonMan
NeonMan

Reputation: 653

Problem throwing and catching custom exceptions in C++

I've created the class Someting which throws an exception SomethingException (SomethingException inherits from std::exception) when it fails to instantiate. the problem is I can't catch SomethingException as such (I had to do a dirty trick to catch it).

There is somewhere in the program where it executes: This doesn't work, the exception is not caught and the program crashes.

try{
    Something* s = new Something();
}
catch (SomethingException* e){
    std::cerr<<e.what();
}

In contrast this does work (exception is caught and the correct message shown) but I really have the feelin I shouldn't be doing this

try{
    Something* s = new Something();
}
catch (std::exception* e){
    SomethingException* e2 = (SomethingException*) e;
    std::cerr<<e.what();
}

Because the pointer is casted I can only make this work if and only if one type of exception is thrown. The moment I need to catch various types this won't work.

Is there a way to caught a custom exception in a more correct way?

Edit:

The exception is thrown as follows

//...
throw new SomethingException ("Errormessage"); //Custom exception constructor
//...

The declaration of Something::Something() is

Something::Something() throw(...)

Using the declaration

Something::Something() throw(SomethingException)
//or
Something::Something() throw(SomethingException*)

Throws a lot of warnings (Warning C4290)

Upvotes: 2

Views: 8891

Answers (3)

Richard Whitehead
Richard Whitehead

Reputation: 896

For others who may have the problem where a custom exception, derived from std::exception, is being thrown but not caught, also check: - That the inheritance is public - If your exception is declared in another DLL, that the exception class is exported from the DLL. Strangely, if it is not, this does not produce a link error (in VS2012), it just fails to be caught.

Upvotes: 7

Jon Purdy
Jon Purdy

Reputation: 54971

In general it's best to throw exceptions by value and catch them by reference:

try {

    throw SomethingException();

} catch (const SomethingException& error) {

    std::cerr << error.what() << '\n';

}

You would only be able to catch an exception with catch (SomethingException*) if you were to throw it with throw new SomethingException(). There isn't enough information in your question to tell, but the problem may be in how SomethingException derives from std::exception. Verify that or change it to inherit from, say, std::runtime_error or std::logic_error instead.

Also, don't use throw specifiers. Just don't. No compiler affords any benefit to using checked exceptions: in effect, checked exceptions aren't checked except to fail horribly (throwing std::bad_exception) in the event of an exception that doesn't conform to the specifier. That's probably what's happening in your code.

Upvotes: 8

Nick
Nick

Reputation: 2050

Can you show the code where you throw the exception.

Another point is about throw-specifications - it's generally a bad idea. The problem is that C++, unlike Java, doesn't insist on throw-specs and so you get next to no benefit from them. All that they can do is potentially cause a core dump if your code (or some code that you call) throws an exception that isn't specified in the throw-spec

Upvotes: 1

Related Questions