I am doing a code review and I have encountered a class in an application that is throwing an exception in the constructor:
class QueueMessage
{
private:
std::string m_bucketName;
std::string m_objectName;
public:
QueueMessage(const std::string& messageIn)
{
std::stringstream ss;
ss << messageIn;
PTree pt;
json::read_json(ss, pt);
m_bucketName = pt.get<std::string>("bucket");
m_objectName = pt.get<std::string>("path");
if (m_bucketName.empty() || m_objectName.empty())
{
throw QueueMessageException("Empty fields in queue message");
}
}
std::string getBucketName() const { return m_bucketName; }
std::string getObjectName() const { return m_objectName; }
};
The class is used to store the message from the queue (that is a JSON-like text) in its members. Is it ok to throw an exception in the constructor if the members of the class are string
, int
, vector
, smart pointers
, etc.?
*
) allocated withnew
ormalloc
? \$\endgroup\$std::unique_ptr
to manage the dynamically allocated memory. \$\endgroup\$