static void Main(string[] args) { Console.WriteLine("***** Simple exception example *****\n"); Console.WriteLine("=> Creating a car and stepping on it!"); Car myCar = new Car("Zippy", 20); myCar.CrankTunes(true); //speed up beyond MaxSpeed to trigger our exception try { for (int i = 0; i < 10; i++) myCar.Accelerate(10); } catch (Exception e) { Console.WriteLine("\n*** Error! ***"); Console.WriteLine("Member name: {0}", e.TargetSite); Console.WriteLine("Class defining member {0}", e.TargetSite.DeclaringType); Console.WriteLine("Member type: {0}", e.TargetSite.MemberType); Console.WriteLine("Message: {0}", e.Message); Console.WriteLine("Source: {0}", e.Source); Console.WriteLine("Stack: {0}", e.StackTrace);//NIIICE Console.WriteLine("Help Link: {0}", e.HelpLink); Console.WriteLine("\n->Custom Data:"); foreach (DictionaryEntry de in e.Data) { Console.WriteLine("->{0}: {1}", de.Key, de.Value); } } //the error has been handled, so processing continues Console.WriteLine("\n***** Out of exception logic *****"); Console.ReadLine(); }
static void Main(string[] args) { Car myCar = new Car("Zippy", 20); myCar.CrankTunes(true); try { for (int i = 0; i < 10; i++) { myCar.Accelerate(10); } } catch (Exception e) { Console.WriteLine("Error!"); Console.WriteLine("TargetSite: {0}", e.TargetSite); Console.WriteLine("Class defining: {0}", e.TargetSite.DeclaringType); Console.WriteLine("Member type: {0}", e.TargetSite.MemberType); Console.WriteLine("Message: {0}", e.Message); Console.WriteLine("Source: {0}", e.Source); Console.WriteLine("StackTrace: {0}", e.StackTrace); Console.WriteLine("HelpLink: {0}", e.HelpLink); if (e.Data != null) { Console.WriteLine("Custom Data:"); foreach (DictionaryEntry de in e.Data) { Console.WriteLine("\t {0} -> {1}", de.Key, de.Value); } } } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Simple Exception Example *****"); Console.WriteLine("=> Creating a car and stepping on it!"); Car myCar = new Car("Zippy", 20); myCar.CrankTunes(true); // Speed up past the car's max speed to // trigger the exception. try { for (int i = 0; i < 10; i++) { myCar.Accelerate(10); } } catch (Exception e) { Console.WriteLine("\n*** Error! ***"); Console.WriteLine("Member name: {0}", e.TargetSite); Console.WriteLine("Class defining member: {0}", e.TargetSite.DeclaringType); Console.WriteLine("Member type: {0}", e.TargetSite.MemberType); Console.WriteLine("Message: {0}", e.Message); Console.WriteLine("Source: {0}", e.Source); Console.WriteLine("\nStack: {0}\n", e.StackTrace); Console.WriteLine("Help Link: {0}", e.HelpLink); // By default, the data field is empty, so check for null. Console.WriteLine("\n->Custom Data:"); if (e.Data != null) { foreach (DictionaryEntry de in e.Data) { Console.WriteLine("-> {0}: {1}", de.Key, de.Value); } } } // The error has been handled, processing continues with the next statement. Console.WriteLine("\n***** Out of exception logic *****"); Console.ReadKey(); }
static void Main(string[] args) { Car myCar = new Car("Zippy", 20); myCar.CranckTunes(true); try { for (int i = 0; i < 10; i++) { myCar.Accelerate(10); } } catch (Exception ex) { Console.WriteLine("\n*** Error! ***"); Console.WriteLine("Method: {0}", ex.TargetSite); Console.WriteLine("Class: {0}", ex.TargetSite.DeclaringType); Console.WriteLine("Type: {0}", ex.TargetSite.MemberType); Console.WriteLine("Message: {0}", ex.Message); Console.WriteLine("Source: {0}", ex.Source); Console.WriteLine("Link: {0}\n", ex.HelpLink); if (ex.Data != null) { foreach (DictionaryEntry de in ex.Data) { Console.WriteLine("-> {0}: {1}", de.Key, de.Value); } } Console.WriteLine("StackTrace: {0}", ex.StackTrace); } Console.WriteLine("\n******** Out of exception logic ********"); Console.ReadLine(); }