Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looser throw specifier error in C++

Tags:

c++

exception

The following code is generating the "Looser throw specifier error". Could you please help me to overcome this error?

class base
{
    virtual void abc() throw (exp1);
}

void base::abc() throw (exp1)
{
    ......
}

class sub : public base
{
    void abc() throw(exp1, exp2);
}

void sub::abc() throw (exp1, exp2)
{
    .....
}
like image 260
rajan Avatar asked Mar 11 '11 07:03

rajan


1 Answers

The problem comes about because the subclass must be usable wherever the base class can be used, and so must not throw any exception types other than those specified in the base class.

There are three solutions:

  1. Modify the base class specifier to include every exception type that any subclass might ever need to throw
  2. Modify every subclass to handle every exception type except those specified in the base class
  3. Remove the exception specifiers.

I would suggest removing them; they are widely regarded as a bad idea, partly because of issues like this. As Matthieu points out, the Standard Committee agrees, and exception specifiers are due to be deprecated in the next version of the Standard.

like image 94
Mike Seymour Avatar answered Oct 17 '22 15:10

Mike Seymour