views:

389

answers:

2

I am getting an error that says:

error: looser throw specifier for 'virtual CPLAT::CP_Window::~CP_Window()'

On the destructor, I have never heard of this before and some Google Searches say this might be a GCC 4 problem, which I would not be sure how to work around since I need GCC 4 to build a Universal Binary.

My Environment: OS X 10.6, XCode 3.2.2, GCC 4 to build a universal binary.

What is the issue?

+5  A: 

http://www.agapow.net/programming/cpp/looser-throw-specifier

Did you put throw() after the declaration of ~CP_Window() ?

Top link in google search "looser throw specifier" BTW.

Noah Roberts
+6  A: 

I assume that CPLAT has a base class? I'm also guessing that you did not put a throw specifier on CPLAT's destructor?

You can put throw(X) (where X is a comma-separated list of exceptions) at the end of a function's signature to indicate what exceptions it's allowed to throw. If you put throw() as the throw specifier, then it would indicate that no exceptions can be thrown from that function. It's fairly common to do this with destructors since you don't ever want a destructor to throw an exception.

A class that overrides a function which has a throw specifier cannot have a looser throw specifier (list more exceptions) than the function being overridden, since that would indicate that the derived class' function could violate the throw specifier of the base class' function. Not having a throw specifier means that any exception can be thrown from that function, so it's as loose as it can get.

In all likelihood, you need to add throw() to the end of the function signature of CPLAT's destructor.

Edit: By the way, I should probably add that you probably don't want to use throw specifiers (other than throw() on destructors) without really knowing that that's what you want. Unlike Java's checked exceptions, they're not caught at compile-time but rather terminate your program at runtime if violated. So, it's best not to use them unless you know what you're doing.

Jonathan M Davis
My stl implementation declares std::exception's destructor as `~exception() throw();`Now if I go and use this as a base for my own exceptions, I have to declare a desctructor in every derived class even if there is nothing to do in that destructor because if I don't, gcc complains. Is there a way to avoid this redundant declaration?
Moritz Both
Not that I know of, if nothing else, because it's technically not redundant. If you don't define a destructor, you get one which has no throw specifier. So, it has the wrong function signature. You have to give it the correct function signature by defining it with an empty throw specifier: `throw()`. It would be nice if the compiler just did that for you, but it's not that smart apparently.
Jonathan M Davis
Thanks for the comment.
Moritz Both