I have a class called Person where I use a special variable as a status, if some atributes have wrong values it creates an object with status error. I check the atributes in the class constructor using if-else statement. How can I implement this using throw-catch exceptions? For example I want to check that string values (name and lastName) are not empty and contain only letters
enum Status {
OK, Error
};
Class Person {
private:
string name;
string lastName;
int age;
public:
Person(string name, string lastName, int age);
Person() {Status = Error;}
~City();
};
Person::Person(string name, string lastName, int age) {
if (
name.empty() || //no person with empty name
lastName.empty() || //no person with empty last name
age <= 0 || //age is a positive number
) {
status = Error; //wrong parameters, object created with status error
} else {
this->name = name;
this->lastName = lastName;
this->age = age;
status = OK;
}
}