title: C++ Exceptions with message
body: <p>I'm not sure that my custom exception approach is correct. What I want to do is to throw exceptions with custom messages but it seems that I created a memory leak...</p>
  
  <pre><code>class LoadException: public std::exception {
  private:
      const char* message;
  public:
      LoadException(const std::string message);
      virtual const char* what() const throw();
  };
  
  
  LoadException::LoadException(const std::string message) {
      char* characters = new char[message.size() + 1];
      std::copy(message.begin(), message.end(), characters);
      characters[message.size()] = '\0';
      this-&gt;message = characters;
  }
  </code></pre>
  
  <p>I use it as follows:</p>
  
  <pre><code>void array_type_guard(Local&lt;Value&gt; obj, const std::string path) {
      if (!obj-&gt;IsArray()) {
          throw LoadException(path + " is not an array");
      }
  }
  
  try {
      objects = load_objects();
  } catch (std::exception&amp; e) {
      ThrowException(Exception::TypeError(String::New(e.what())));
      return scope.Close(Undefined());
  }
  </code></pre>
  
  <p>I afraid that the array created in constructor is never deleted. But I'm not sure how to delete it - should I add destructor or maybe use completely different approach?</p>
  
  <p><em>Update</em>:</p>
  
  <p>I've actually tried to use the string class as follows:</p>
  
  <pre><code>class LoadException: public std::exception {
  private:
      const char* msg;
  public:
      LoadException(const std::string message);
      virtual const char* what() const throw();
  };
  
  LoadException::LoadException(const std::string message) {
      msg = message.c_str();
  }
  
  const char* LoadException::what() const throw() {
      return msg;
  }
  </code></pre>
  
  <p>But cannot get the error message then - some random output is displayed when I print the "what()".</p>