Question

I have two exception classes, with one inheriting from the other:

class bmd2Exception : public std::runtime_error
{
  public:
    bmd2Exception(const std::string & _description) throw () : std::runtime_error(_description) {}
    ~bmd2Exception() throw() {}
};

class bmd2FileException : public bmd2Exception
{
  public:
    bmd2FileException(const std::string & _description, const char * _file, long int _line) throw()
    {
      std::stringstream ss;
      ss << "ERROR in " << _file << " at line " << _line << ": " << _description;
      bmd2Exception(ss.str());
    }
    ~bmd2FileException() throw() {}
};

The error message I get:

no matching function for call to ‘bmd2::bmd2Exception::bmd2Exception()’

I understand this is because the constructor for bmd2FileException is trying to call bmd2Exception(), which has not been defined. What I really want to happen is for bmd2FileException() to call bmd2Exception(const std::string &) with the concatenated error message. How do I do this?

Thank you!

Was it helpful?

Solution

One common paradigm is to create a helper function:

class bmd2FileException : public bmd2Exception
{
    private:
        static std::string make_msg(const std::string & _description, const char * _file, long int _line);

    public:
        bmd2FileException(const std::string & _description, const char * _file, long int _line)
            : bmd2Exception(make_msg(_description, _file, _line))
        { }      
};

Now just put your message-creating code in bmd2FileException::make_msg(...).

By the way, if your constructor is concatenating strings, I wouldn't be so sure it is throw().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top