Working on a project, new to c# here, and I'm trying to take care of unhanded exceptions. What I'm trying to do is give the user a helpful error message whenever they type something that isn't one of the choices and keep prompting them until they enter a valid response.
string input = Console.ReadLine();
// bool userBool = false;
// while( userBool){
// }
switch (Int32.Parse(input))
{
case 1:
farm.AddGrazingField(new GrazingField());
Console.WriteLine("Your Facility has been added");
break;
case 2:
farm.AddPlowedField(new PlowedField());
Console.WriteLine("Your Facility has been added");
break;
case 3:
farm.AddNaturalField(new NaturalField());
Console.WriteLine("Your Facility has been added");
break;
case 4:
farm.AddChickenHouse(new ChickenHouse());
Console.WriteLine("Your Facility has been added");
break;
case 5:
farm.AddDuckHouse(new DuckHouse());
Console.WriteLine("Your Facility has been added");
break;
default:
break;
}
I know I could do this with a while loop and conditionals but havent been successful doing that with switch case.