3

I'm quite iffy when it comes to C++ std:exception handling. Here's some sample code I found on the web, which I currently use.

class MyBaseException : public std::exception
{
  public:

  explicit MyBaseException(const std::string & message)
     : m_Base(message.c_cstr()) {}

  explicit MyBaseException(const char *message)
     : m_Base(message) {}

  virtual ~MyBaseException() throw () {}

protected:
  typedef std::exception m_Base;
};

class MyDerivedException : public MyBaseException
{
  public:

  explicit MyDerivedException (const std::string & message)
     : m_Base(message.c_cstr()) {}

  explicit MyDerivedException (const char *message)
     : m_Base(message) {}

  virtual ~MyDerivedException () throw () {}

protected:
  typedef MyBaseException m_Base;
};

Now, what I'd like to do is to automatically prepend every exceptions raised with the following scheme.

Some code raises a MyDerivedException exception with the following: "original_exception_message"

When the MyDerivedException receives "original_exception_message", I'd like to prepend it with: "Derived Exception Raised: "

And when MyBaseException receives the MyDerivedException exception, I'd like to prepend it with: "Base Exception Raised: "

Such that the final message would look like this:

"Base Exception Raised: Derived Exception Raised: original_exception_message"

I gotta feeling I'm going to get all sorts of nasty replies on this, about bad concepts and bad practices... But I don't claim to be an expert.

Note that the prepend messages aren't actually that. They would be a little more informative.

Thanks in advance.

ThermoX
  • 277
  • 3
  • 11
  • 1
    "I gotta feeling I'm going to get all sorts of nasty replies on this, about bad concepts and bad practices" You know this C++ community too well :-). Good questions, however, get good responses! – AndyG Jul 15 '16 at 15:24
  • 2
    Am I missing something, or are you declaring the same constructor twice? (`const char *message`) – lcs Jul 15 '16 at 15:41
  • You've got duplicated constructors - the above code will fail to compile. – Steve Lorimer Jul 15 '16 at 15:42
  • @Ics Fixed. Thanks. – ThermoX Jul 15 '16 at 16:00

1 Answers1

1
#include <iostream>
#include <exception>

class MyBaseException : public std::exception
{
public:    
  explicit MyBaseException(const std::string & message)
     : m_message("Base Exception Raised: " + message) {}

  virtual const char* what() const throw ()
  {
      return m_message.c_str();
  }

private:
  const std::string m_message;
};

class MyDerivedException : public MyBaseException
{
public:

  explicit MyDerivedException (const std::string& message)
     : MyBaseException("Derived Exception Raised: " + message) {}

};

int main()
{
    try
    {
        throw MyDerivedException("derived");
    }
    catch(std::exception const& e)
    {
        std::cout << e.what();
    }
    return 0;
}

And read this link http://en.cppreference.com/w/cpp/error/exception

mooncheese
  • 124
  • 1
  • 1
  • 4
  • Thanks mooncheese. I had been playing around with a similar approach. But my version still kept the : m_Base(message) part... which wasn't giving me the expected results (mostly cause I don't know what the heck I'm doing). But I understand a little better now. I'll mark you up as the answer, if nobody else brings up an issue with that approach (like today). Thanks again. – ThermoX Jul 15 '16 at 16:53