User entered value validation and level of error catching

May I ask should the error catching code be placed at the lowest level or at the top as I am not sure what is the best practice? I prefer placing at the bottom, example a, as

Example a

   public static void Main(string[] args)
    {
        string operation = args[0];
        int value = Convert.ToInt32(args[1]);
        if (operation == "date")
        {
            DoDate(value);
        }
        else if (operation == "month")
        {
            DoMonth(value);
        }
    }
    public static void DoMonth(int month)
    {
        if (month < 1 || month > 12)
        {
            throw new Exception("");
        }
    }
    public static void DoDate(int date)
    {
        if (date < 1 || date > 31)
        {
            throw new Exception("");
        }
    }

or example b

   public static void Main(string[] args)
    {
        string operation = args[0];
        int value = Convert.ToInt32(args[1]);
        if (operation == "date" && (date < 1 || date > 12))
        {
            throw new Exception("");
        }
        else if (operation == "month" && (month < 1 || month > 31))
        {
            throw new Exception("");
        }
        if (operation == "date")
        {
            DoDate(value);
        }
        else if (operation == "month")
        {
            DoMonth(value);
        }
    }
    public static void DoMonth(int month)
    {
    }
    public static void DoDate(int date)
    {
    }

Answers


there is a concept that will help you understand the consequences for each of your example

separation of concerns

http://en.wikipedia.org/wiki/Separation_of_concerns


It depends. The technical validation of input should be done in front of the consuming methode. Technical validation means things like: Are there enough parameter? Perhaps: Do they have the right "type"?

The information validation should be done right before the input is interpreted. So you are conform with the OO principles.


Need Your Help

Why does this code not work in IE & Safari?

javascript html browser

I've been tasked with writing some code, that places a small icon in the top right corner of an Ad regardless of size. Depending on the language of the user (in this case I'm using the browser lang...