User entered value validation and level of error catching
- by Terry
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)
{
}