views:

245

answers:

3

If so how?

I know how to provide exception specifications for members such as

class SOMEClass
{
public:


   void method(void)  throw (SOMEException); 

   virtual void pure_method(void) = 0;
};

So that the method throws only SOMEException. If I want to ensure that sub-classes of SOMEClass throw SOMEException for pure_method, is it possible to add the exception specification?. Is this approach feasible or do I need to understand more on exceptions and abstract methods to find out why it can(not) be done?

A: 
    virtual void action() throw() = 0;

It is possible. But reasonable only for throw() case. Compiler will warn you every time derived class forgets add "throw()" specification on its "action" method declaration.

Mykola Golubyev
+1  A: 

Yes, I'm pretty sure put an exception specification on a pure virtual function although I haven't tried it.

However, most C++ experts agree that apart from the nothrow specifications, C++ exception specifications are pretty useless and while they are a hint to the compiler, they are not enforced the same way that they are in, for example, Java.

Unless you put the appropriate catch-all block into each and every implementation of your pure virtual function, you simply cannot guarantee that it will only throw the exceptions listed in your exception specification.

Timo Geusch
+6  A: 

Yes, a pure virtual member can have an exception specification.

I recommend you to read this: http://www.gotw.ca/publications/mill22.htm before getting too much involved in exception specifications, though.

Éric Malenfant
Thanks all for the responses. I suspected it may not be a good idea especially that I am just beginning to fully use exceptions as required on our project. I will explore boost exceptions as well
MeThinks