mercredi 2 mai 2018

Corrupted message in custom exception

I'm trying to implement a custom class exception.

The exception itself works, but I recieve a corrupted output

#include <stdexcept>

namespace Exception{

class LibraryException : public std::runtime_error
{
public:
   explicit LibraryException(const std::string& message)
      : std::runtime_error(""),
        prefix_("LibraryException: "),
        message_(message)
   {
   }

   const char* what() const noexcept override
   {
      std::string out;
      out += prefix_;
      out += message_;
      return out.c_str();
   }

private:
   std::string prefix_;
   std::string message_;
};

class BadSizeException : public LibraryException
{

public:
   explicit BadSizeException() : LibraryException("Library: Bad Size\n")
   {
   }
};
}

Output when I try to raise the exception:

°áó°áóxception: Bad Size

What I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire