private static void ReadLastName(Person tempPerson) { while (true) { Console.WriteLine("Please enter person's last name:"); try { string currentName = Console.ReadLine(); tempPerson.LastName = currentName; break; } catch (ArgumentNullException ex) { Console.Error.WriteLine("Exception: {0}", ex.Message); } } }
public static void Main() { //Person pesho = new Person("Ivo", "Ivov", 28); try { Person noname = new Person(string.Empty, "pepov", 88); } catch(ArgumentNullException ex) { Console.WriteLine("Exception thrown: {0}", ex.Message); } catch (ArgumentOutOfRangeException ex) { Console.WriteLine("Exception thrown: {0}", ex.Message); } //Person nolast = new Person("Pepo", null, 55); //Person negativ = new Person("Gogo", "Gogov", -5); //Person old = new Person("Ivo", "Gogov", 121); }
static void Main(string[] args) { List<Person> collection = new List<Person>(); while (true) { Person tempPerson = new Person(); ReadFirstName(tempPerson); ReadLastName(tempPerson); ReadAge(tempPerson); collection.Add(tempPerson); Console.WriteLine("Person added!"); break; } }
private static void ReadAge(Person tempPerson) { while (true) { Console.WriteLine("Please enter person's age:"); try { int temp = int.Parse(Console.ReadLine()); tempPerson.Age = temp; break; } catch (ArgumentNullException ex) { Console.Error.WriteLine("Exception: {0}", ex.Message); } catch (ArgumentOutOfRangeException ex) { Console.Error.WriteLine("Exception: {0}.", ex.Message); } } }