custom exception class, using a base class, with multiple arguments
Solution 1:
As Silvio mentioned in another comment, you can't use strcat
if the buffer can't contain the resulting string. In this case, you also have the problem that you're passing a const char *
to strcat
which is a no no (literal strings are const).
I suggest that you change your msg
variable to a std::string
. It will then be able to grow to accommodate the concatenated message, and manage the memory for you.
class AgentException : public std::exception
{
protected:
std::string msg;
public:
AgentException() : msg("AgentException") {};
AgentException(const char *m) : msg(m){};
AgentException(const char *m, const std::string& d)
{
msg = m + d;
};
~AgentException() = default;
const char *what() const throw()
{
return msg.c_str();
}
};
class ConnectionFailed : public AgentException
{
public:
ConnectionFailed() : AgentException("ConnectionFailed"){};
ConnectionFailed(const std::string& d) : AgentException("ConnectionFailed: ", d){};
~ConnectionFailed() = default;
};