Consider a scenario where a console application asks for a series of inputs, one after the other. Each input is validated before proceeding to the next. Now if an input made by the user is invalid, an error message is shown and the user is asked if he/she wants to continue. If the user chooses no, the input process is aborted. If the user chooses yes, the user is allowed to retype the input again, and the process continues by asking for the remainder of the inputs.
If I used a read() method to handle the user inputs (which also validates the input and asks if the user wishes to continue [y/n]) that returns false if the user chooses not to continue, then I would have to check for the result of every read() call before proceeding to the next
For example:
bool valid = read("Name");
if (valid) valid = read("Age");
if (valid) valid = read("Sex");
if (valid) valid = read("DoB");
if(valid)
{
// do stuff
}
Alternatively, I can have the read() method throw an exception if the user chooses to discontinue, in which case my code would become:
try {
read("Name");
read("Age");
read("Sex");
read("DoB");
// do stuff
}catch(input_exception& e)
{}
Which of these approaches would you choose and why? (Assume that the read() method throws the exception properly after cleaning up)
NOTE: I'm not looking for arguments for and against exceptions, which is abundantly available on SO. I'm only looking for an answer to the specific question.