Isn't it better to use a single try catch instead of tons of TryParsing and other error handling sometimes?

Posted by Ryan Peschel on Stack Overflow See other posts from Stack Overflow or by Ryan Peschel
Published on 2012-11-03T15:12:40Z Indexed on 2012/11/03 17:01 UTC
Read the original article Hit count: 269

I know people say it's bad to use exceptions for flow control and to only use exceptions for exceptional situations, but sometimes isn't it just cleaner and more elegant to wrap the entire block in a try-catch?

For example, let's say I have a dialog window with a TextBox where the user can type input in to be parsed in a key-value sort of manner.

This situation is not as contrived as you might think because I've inherited code that has to handle this exact situation (albeit not with farm animals).

Consider this wall of code:

class Animals
{
    public int catA, catB;
    public float dogA, dogB;
    public int mouseA, mouseB, mouseC;
    public double cow;
}

class Program
{
    static void Main(string[] args)
    {
        string input = "Sets all the farm animals CAT 3 5 DOG 21.3 5.23 MOUSE 1 0 1 COW 12.25";

        string[] splitInput = input.Split(' ');

        string[] animals = { "CAT", "DOG", "MOUSE", "COW", "CHICKEN", "GOOSE", "HEN", "BUNNY" };

        Animals animal = new Animals();

        for (int i = 0; i < splitInput.Length; i++)
        {
            string token = splitInput[i];

            if (animals.Contains(token))
            {
                switch (token)
                {
                    case "CAT":
                        animal.catA = int.Parse(splitInput[i + 1]);
                        animal.catB = int.Parse(splitInput[i + 2]);
                        break;
                    case "DOG":
                        animal.dogA = float.Parse(splitInput[i + 1]);
                        animal.dogB = float.Parse(splitInput[i + 2]);
                        break;
                    case "MOUSE":
                        animal.mouseA = int.Parse(splitInput[i + 1]);
                        animal.mouseB = int.Parse(splitInput[i + 2]);
                        animal.mouseC = int.Parse(splitInput[i + 3]);
                        break;
                    case "COW":
                        animal.cow = double.Parse(splitInput[i + 1]);
                        break;
                }
            }
        }
    }
}

In actuality there are a lot more farm animals and more handling than that. A lot of things can go wrong though. The user could enter in the wrong number of parameters. The user can enter the input in an incorrect format. The user could specify numbers too large or too small for the data type to handle.

All these different errors could be handled without exceptions through the use of TryParse, checking how many parameters the user tried to use for a specific animal, checking if the parameter is too large or too small for the data type (because TryParse just returns 0), but every one should result in the same thing:

A MessageBox appearing telling the user that the inputted data is invalid and to fix it. My boss doesn't want different message boxes for different errors. So instead of doing all that, why not just wrap the block in a try-catch and in the catch statement just display that error message box and let the user try again?

Maybe this isn't the best example but think of any other scenario where there would otherwise be tons of error handling that could be substituted for a single try-catch. Is that not the better solution?

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception-handling