Skip to main content
edited tags; edited title
Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Throw exception in constructor bad or good practice Storing a message from a queue

deleted 5 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I am doing a code review and I have metencountered 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 likeJSON-like text) in its members. I am not sure is Is it ok to throw an exception in the constructor if the members of the class are string, int, vector, smart pointers, etc.?

I am doing a code review and I have met 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. I am not sure is it ok to throw an exception in the constructor if the members of the class are string, int, vector, smart pointers, etc.?

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.?

Source Link
sop
  • 207
  • 3
  • 10

Throw exception in constructor bad or good practice

I am doing a code review and I have met 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. I am not sure is it ok to throw an exception in the constructor if the members of the class are string, int, vector, smart pointers, etc.?