I've heard it said that exceptions and try-catch blocks should not be used to flow-control, so I would like a way to rework this code to avoid that appearance.
I have a method validateTrainingSets, within a neuralNetwork class that does exactly what it's name suggests - validates the prvided training sets (to ensure, among other things, that the number of inputs in the training sets match the number of inputs into the neural network, and that the number of outputs per answer matches the number of final outputs of the neural network).
Because there are many different ways in which the validation can fail, I've devided to leave the throwing of exception up to the validation method itself, and there are three different custom exceptions that can be thrown from the function.
In a method that allows users of the class to manually update the number of neurons per layer, the final bit of code validates and/or updates the training sets (depending on whether or not new training sets were provided), and my question is, how would I improve it?
In the code below, the "true" parameter in the validator tells the validator to set the training sets to null if it fails. (It is intended that if the user did not provide new training sets, but the existing training sets are consistent with the new arrangement to keep them, but to discard them if not).
The fldValidate field is a flag that's set to false earlier in the code and I need to ensure that it's set back to true ragardless of any errors.
try { validateTrainingSets(fldTrainingSets, fldTrainingAnswers, true); }
catch(TrainingSetCardinalityMismatch) { }
catch(InputSetCardinlityMismatch) { }
catch(OutputAnswerCardinalityMismatch) { }
catch {
fldValidate = true;
throw;
}
finally { fldValidate = true; }