How to Handle Incorrect Values in a Constructor? Last Updated : 12 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, constructors are used to construct and initialize the object of its class. If incorrect values are given to the constructor, then it may cause inconsistency in a program. In this article, we will learn how to handle incorrect values in a constructor in C++ Handling Incorrect Arguments for Constructor in C++In C++, the most common method is to handle the incorrect values that are passed to the constructor as the arguments is to throw a std::invalid_argument exception if the arguments are not as desired and catch that exception in the code where the object is being constructed. C++ Program to Handle Incorrect Values in ConstructorThe below example demonstrates how we can handle incorrect values in the constructor by incorporating parameter validation, exception handling, and error reporting. C++ // C++ program to handle incorrect values in a constructor #include <iostream> #include <stdexcept> using namespace std; // class named rectangle class Rectangle { private: double length; double width; public: // Constructor with the parameter validation Rectangle(double l, double w) : length(l) , width(w) { // Validate parameters if (length <= 0 || width <= 0) { throw invalid_argument( "Invalid dimensions. Length and width must " "be positive."); } } // Display dimensions void print() const { cout << "Length: " << length << " units, Width: " << width << " units" << endl; } }; int main() { try { // Creating a Rectangle object with the valid // dimensions Rectangle validRectangle(5.0, 10.0); validRectangle.print(); Rectangle invalidRectangle(0.0, -3.0); invalidRectangle.print(); } // catching exception raised in try block catch (const exception& e) { cerr << "Error: " << e.what() << endl; } return 0; } Output Length: 5 units, Width: 10 units Error: Invalid dimensions. Length and width must be positive. Comment More infoAdvertise with us Next Article How to Define the Default Constructor in C++? M mguru4c05q Follow Improve Article Tags : C++ Programs C++ C++-Constructors C++-Exception Handling CPP-OOPs CPP Examples +2 More Practice Tags : CPP Similar Reads How to Define the Default Constructor in C++? In C++, a constructor that takes no parameters is called a default constructor. A default constructor gets automatically invoked when an object of a class is created without any arguments. It is mainly used to initialize legal initial values to the member variables or perform other setup tasks. In t 2 min read How to Handle Wrong Data Type Input in C++? In C++, when weâre expecting a certain type of input but the user enters a different type then such input is called incorrect data input and it can cause issues like unexpected behavior, program failures, or inaccurate results. In this article, we will learn how to handle wrong data type input in C+ 2 min read How to Declare a Copy Constructor in a Derived Class? In C++, a copy constructor is a constructor that initializes an object as a copy of an existing object of the same class. In this article, we will learn how to declare a copy constructor in a derived class. Declaring a Copy Constructor in a Derived ClassWe can declare a copy constructor in a derived 3 min read How to Implement a Copy Constructor in a Derived Class in C++ In object-oriented programming, a copy constructor is a special member function that initializes a new object as a copy of an existing object. In this article, we will learn how to implement a copy constructor in a derived class. Implementing Copy Constructor in a Derived Class in C++ In C++, when w 3 min read Move Constructors in C++ A move constructor is a special type of constructor in C++ that is used to create a new object from the already existing object of the same type, but instead of making a copy of it, it makes the new object point to the already existing object in the memory, leaving the source object in a valid but u 10 min read Problem with Single Argument Constructor in C++ and How to solve it In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes a conversion constructor because such a constructor allows automatic conversion to the class being constructed. Problem:Whenever there is a constructor with a single argument and there is a 8 min read Like