Search code examples
c++gccthrow

Inline throw() method in C++


I am trying to define a really simple exception class. Because it is so simple I want to keep it in the .h file only, but the compiler doesn't like throw(). The code:

#include <exception>
#include <string>

class PricingException : public virtual std::exception
{
private:
    std::string msg;
public:
        PricingException(std::string message) : msg(message) {}
        const char* what() const throw() { return msg.c_str(); }
        ~PricingException() throw() {}
};

GCC gives the following errors:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:13: error: expected unqualified-id before ‘{’ token
/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: expected unqualified-id before ‘{’ token

for lines with throw(). Any idea how to fix it?

EDIT

I tried to remove the bodies of the problematic methods, i.e.

virtual ~PricingException() throw();// {}

And now I get even more weird error message:

/home/ga/dev/CppGroup/MonteCarlo/PricingException.h:14: error: looser throw specifier for ‘virtual PricingException::~PricingException()’
/usr/include/c++/4.5/exception:65: error:   overriding ‘virtual std::exception::~exception() throw ()’

It just ignored my throw specifier!


Solution

  • Found it finally! @Mike Seymour was right in his comment - it turns out that in a file nr3.h (part of Numerical Recepies code) there is a macro throw(message) defined.

    What I don't understand is why it impacts compilation of files that don't include this .h file...

    Anyway, I think Visual Studio had a different compilation order or something, so it was pure luck that it compiled there and not under gcc.