User entered value validation and level of error catching
Posted
by Terry
on Stack Overflow
See other posts from Stack Overflow
or by Terry
Published on 2010-05-20T10:07:42Z
Indexed on
2010/05/20
10:10 UTC
Read the original article
Hit count: 314
c#
|error-handling
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) { }
© Stack Overflow or respective owner