How to handle exceptions with switch case

Viewed 95

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.

2 Answers

You can use function to both read and also validate the input of the user:

int GetUserInput()
{
    while (true)
    {
        Console.Write("Please enter a number: ");
        var input = Console.ReadLine();
        if (int.TryParse(input, out var value))
            return value;
    }
}

This function will not throw an exception if the user enters an invalid value. Instead it will prompt the user again. You can expand this to limit the range of the input that you allow and write descriptive error messages.

Please observe the below updates in your code.

      try
   
        {
            
            Console.WriteLine("Please enter your input in number form");

            string input = Console.ReadLine();

            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:
                    throw new Exception();
            }
        }
        catch (Exception ex)
        {
            //Can proovide more info to user to make sure he knows the input to be entered or options available to him
            Console.WriteLine("Please verify the input");
        }
        finally
        {
            //As it will get executed in case of exception also you can log some message or perform something here
        }

This is just the beginning. You can have your custom exceptions and use them for logging messages.

Related