2

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)

3
  • Why are you throwing a pointer instead of by value? Commented Jun 12, 2011 at 21:13
  • Your code feels like you are a Java programmer starting in C++. The way you use exceptions is not the usual C++ way. Commented Jun 12, 2011 at 21:51
  • Note: It is easier to derive exceptions from std::runtime_error than std::exception. This is because std::runtime_error will store the error message (passed in the constructor) for you that is returned by the what() method. Note: MSVC has a non standard std::exception that allows you to pass the error message in the constructor. Commented Jun 12, 2011 at 23:01

3 Answers 3

8

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.

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

11 Comments

> throw new SomethingException() is the way is thrown. I'll try what you say anyway
This time it doesn't compile at all. 'std::runtime_exception doesn't have a default constructor' also the compiler throws a warning saying 'omitting exception specification except to assert the function is not __declspec(nothrow)' <-- rough translation
@NeonMan: The runtime_error and logic_error constructors take a string message, and what() must have the throw() specifier if you overload it.
@NeonMan: You should get rid of the throw(SomethingException) specifier on the function. There's really no reason catching a user-defined exception should fail to work, and there's not enough information in your post to tell where your problem is.
@NeonMan: I suggest no throw specifier whatsoever on the function declaration and, yes, throw SomethingException(); in the function body.
|
7

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.

1 Comment

This helped with GCC too (adding public after : in custom exception declaration).
1

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

1 Comment

Added the throw line. I really like the way java uses the exceptions, unlike c++. Preventing an object for being constructed is essential in this case and, using a custom exxception is preferred.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.