// See if Car has overheated. // This time, throw an exception if the user speeds up beyond MaxSpeed. public void Accelerate(int delta) { if (carIsDead) Console.WriteLine("{0} is out of order...", PetName); else { CurrentSpeed += delta; if (CurrentSpeed >= MaxSpeed) { carIsDead = true; CurrentSpeed = 0; // We need to call the HelpLink property, thus we need // to create a local variable before throwing the Exception object. CarIsDeadException ex = new CarIsDeadException(string.Format("{0} has overheated!", PetName), "You have a lead foot", DateTime.Now); ex.HelpLink = "http://www.CarsRUs.com"; // Stuff in custom data regarding the error. ex.Data.Add("TimeStamp", string.Format("The car exploded at {0}", DateTime.Now)); ex.Data.Add("Cause", "You have a lead foot."); throw ex; } else Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed); } }
// See if Car has overheated. // This time, throw an exception if the user speeds up beyond MaxSpeed. public void Accelerate(int delta) { if (carIsDead) { Console.WriteLine("{0} is out of order...", PetName); } else { CurrentSpeed += delta; if (CurrentSpeed >= MaxSpeed) { carIsDead = true; CurrentSpeed = 0; // We need to call the HelpLink property, thus we need // to create a local variable before throwing the Exception object. CarIsDeadException ex = new CarIsDeadException(string.Format("{0} has overheated!", PetName), "You have a lead foot", DateTime.Now); ex.HelpLink = "http://www.CarsRUs.com"; // Stuff in custom data regarding the error. ex.Data.Add("TimeStamp", string.Format("The car exploded at {0}", DateTime.Now)); ex.Data.Add("Cause", "You have a lead foot."); throw ex; } else { Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed); } } }