I have a question regarding throwing errors inside a constructor.
So lets say I have a class called Item, which holds three data:
string name, code;
double price;
In my constructor, I pass in three parameters which will assign code, name, price with what the user wants.
I want to have an error handling in my constructor, so if the user enters a price that is negative, an error will be thrown.
If the user enters an invalid code format, the error will be thrown.
My question is, what is the proper format for this?
1 2 3 4 5 6 7 8 9 10 11 12
|
Item (string &name, string &code, double &price)
{
try
{
name = name;
code = code;
price = price;
}
if (price < 0)
{
throw error
}
|
Is this the correct way to do it? So if price is less than 0, then the object shouldnt get created.