/* * 1. Add the required classes to make the following code compile. * HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address. * * 2. Run the program and observe the exception. * * 3. Add try/catch blocks in the appropriate locations to prevent the program from crashing * Print meaningful error messages in the catch blocks. */ static void Main(string[] args) { // Create a few contacts Contact bob = new Contact() { FirstName = "Bob", LastName = "Smith", Email = "*****@*****.**", Address = "100 Some Ln, Testville, TN 11111" }; Contact sue = new Contact() { FirstName = "Sue", LastName = "Jones", Email = "*****@*****.**", Address = "322 Hard Way, Testville, TN 11111" }; Contact juan = new Contact() { FirstName = "Juan", LastName = "Lopez", Email = "*****@*****.**", Address = "888 Easy St, Testville, TN 11111" }; // Create an AddressBook and add some contacts to it AddressBook addressBook = new AddressBook(); try { addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Try to addd a contact a second time addressBook.AddContact(sue); } catch { Console.WriteLine("Contact already exists in address book."); } //without try/catch, this error occurs: //Unhandled Exception: System.ArgumentException: An item with the same key has already been added. //Key: [email protected] // Create a list of emails that match our Contacts List <string> emails = new List <string>() { "*****@*****.**", "*****@*****.**", "*****@*****.**", }; // Insert an email that does NOT match a Contact emails.Insert(1, "*****@*****.**"); // Search the AddressBook by email and print the information about each Contact //without try/catch, this error occurs (line with contact.FullName): //Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. //from microsoft docs: a NullReferenceException is thrown when you try to access //a member on a type whose value is null foreach (string email in emails) { try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {contact.FullName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); } catch (System.NullReferenceException) { Console.WriteLine("Null problems sorry!"); } } }
/* * 1. Add the required classes to make the following code compile. * HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address. * * 2. Run the program and observe the exception. * * 3. Add try/catch blocks in the appropriate locations to prevent the program from crashing * Print meaningful error messages in the catch blocks. */ static void Main(string[] args) { // Create a few contacts Contact garrett = new Contact() { FirstName = "Garrett", LastName = "Kent", Email = "*****@*****.**", Address = "100 Bro Ln, Broville, TN 11111" }; Contact keith = new Contact() { FirstName = "Keith", LastName = "Rutkowski", Email = "*****@*****.**", Address = "666 Bitch St, Bitchville, TN 00000" }; Contact jon = new Contact() { FirstName = "Jon", LastName = "Riley", Email = "*****@*****.**", Address = "100 Precedence Way, Squadville, TN 11111" }; // Create an AddressBook and add some contacts to it AddressBook addressBook = new AddressBook(); addressBook.AddContact(garrett); addressBook.AddContact(keith); addressBook.AddContact(jon); // Try to addd a contact a second time addressBook.AddContact(keith); // Create a list of emails that match our Contacts List <string> emails = new List <string> () { "*****@*****.**", "*****@*****.**", "*****@*****.**", }; // Insert an email that does NOT match a Contact emails.Insert(1, "*****@*****.**"); // Search the AddressBook by email and print the information about each Contact foreach (string email in emails) { try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {contact.FullName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); } catch (KeyNotFoundException) { Console.WriteLine("----------------------------"); Console.WriteLine("We live in a society!"); } } }
/* * 1. Add the required classes to make the following code compile. * HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address. * * 2. Run the program and observe the exception. * * 3. Add try/catch blocks in the appropriate locations to prevent the program from crashing * Print meaningful error messages in the catch blocks. */ static void Main(string[] args) { // Create a few contacts Contact bob = new Contact() { FirstName = "Bob", LastName = "Smith", Email = "*****@*****.**", Address = "100 Some Ln, Testville, TN 11111" }; Contact sue = new Contact() { FirstName = "Sue", LastName = "Jones", Email = "*****@*****.**", Address = "322 Hard Way, Testville, TN 11111" }; Contact juan = new Contact() { FirstName = "Juan", LastName = "Lopez", Email = "*****@*****.**", Address = "888 Easy St, Testville, TN 11111" }; // Create an AddressBook and add some contacts to it AddressBook addressBook = new AddressBook(); addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Try to addd a contact a second time try { addressBook.AddContact(sue); } catch (ArgumentException) { Console.WriteLine("This person already exists in address book"); } // Create a list of emails that match our Contacts List <string> emails = new List <string>() { "*****@*****.**", "*****@*****.**", "*****@*****.**", }; // Insert an email that does NOT match a Contact emails.Insert(1, "*****@*****.**"); // Search the AddressBook by email and print the information about each Contact foreach (string email in emails) { try { Contact contact = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {contact.FullName}"); Console.WriteLine($"Email: {contact.Email}"); Console.WriteLine($"Address: {contact.Address}"); Console.WriteLine("----------------------------"); } catch (KeyNotFoundException) { Console.WriteLine($"{email} could not be found."); } } }
/* * 1. Add the required classes to make the following code compile. * HINT: Use a Dictionary in the AddressBook class to store Contacts. The key should be the contact's email address. * * 2. Run the program and observe the exception. * * 3. Add try/catch blocks in the appropriate locations to prevent the program from crashing * Print meaningful error messages in the catch blocks. */ static void Main(string[] args) { // Create a few contacts Contacts bob = new Contacts() { FirstName = "Bob", LastName = "Smith", Email = "*****@*****.**", Address = "100 Some Ln, Testville, TN 11111" }; Contacts sue = new Contacts() { FirstName = "Sue", LastName = "Jones", Email = "*****@*****.**", Address = "322 Hard Way, Testville, TN 11111" }; Contacts juan = new Contacts() { FirstName = "Juan", LastName = "Lopez", Email = "*****@*****.**", Address = "888 Easy St, Testville, TN 11111" }; // Create an AddressBook and add some contacts to it AddressBook addressBook = new AddressBook(); addressBook.AddContact(bob); addressBook.AddContact(sue); addressBook.AddContact(juan); // Try to addd a contact a second time try{ addressBook.AddContact(sue); } catch { Console.WriteLine("uh oh, you made a mistake sir... or miss"); } // Create a list of emails that match our Contacts List <string> emails = new List <string>() { "*****@*****.**", "*****@*****.**", "*****@*****.**", }; // Insert an email that does NOT match a Contact emails.Insert(1, "*****@*****.**"); // Search the AddressBook by email and print the information about each Contact foreach (string email in emails) { try { // List<string> emailIds = new List<string>() {0, 1, 2, 3}; { Contacts Contacts = addressBook.GetByEmail(email); Console.WriteLine("----------------------------"); Console.WriteLine($"Name: {Contacts.FullName}"); Console.WriteLine($"Email: {Contacts.Email}"); Console.WriteLine($"Address: {Contacts.Address}"); } } catch (KeyNotFoundException) { Console.WriteLine("-----------------"); Console.WriteLine("Something went wrong!"); } } }