コード例 #1
0
        // See if Car has overheated.
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
            }

            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($"{PetName} has overheated!",
                                               "You have a lead foot", DateTime.Now);
                    ex.HelpLink = "http://www.CarsRUs.com";
                    throw ex;
                }
                else
                {
                    Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
コード例 #2
0
        // Throw an exception if the user speeds up beyond MaxSpeed.
        public void Accelerate(int delta)
        {
            if (delta < 0)
                throw new ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
            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 regading 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);
            }
        }
コード例 #3
0
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new ArgumentOutOfRangeException("delta", "Speed must be greater than zero");
            }
            if (carIsDead)
            {
                Console.WriteLine($"{PetName} is out of order");
            }
            else
            {
                CurrentSpeed += delta;
                if (CurrentSpeed > MaxSpeed)
                {
                    CurrentSpeed = 0;
                    carIsDead    = true;

                    CarIsDeadException ex = new CarIsDeadException($"{PetName} was overheated!", "You have a lead foot", DateTime.Now);
                    ex.HelpLink = "http://www.Cars.com";
                    throw ex;
                }
                else
                {
                    Console.WriteLine($"=> Current speed = {CurrentSpeed}.");
                }
            }
        }
コード例 #4
0
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new
                      ArgumentOutOfRangeException("delta", "Speed must be greater than zero");
            }

            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order...", PetName);
            }
            else
            {
                CurrentSpeed += delta;
                if (CurrentSpeed > MaxSpeed)
                {
                    CurrentSpeed = 0;
                    carIsDead    = true;
                    CarIsDeadException ex = new CarIsDeadException(string.Format("{0} has over heated!", PetName), "You have a lead foot", DateTime.Now);
                    ex.HelpLink = "http://carsRus.com";
                    ex.Data.Add("TimeStamp", string.Format("The car exploded at {0}", DateTime.Now));
                    ex.Data.Add("cause", string.Format("You have a lead foot"));
                    throw ex;
                }
                else
                {
                    Console.WriteLine("=> currentSpeed = {0}", CurrentSpeed);
                }
            }
        }
コード例 #5
0
        // See if Car has overheated.
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
            }

            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order...", PetName);
            }
            else
            {
                CurrentSpeed += delta;
                if (CurrentSpeed >= MaxSpeed)
                {
                    carIsDead    = true;
                    CurrentSpeed = 0;

                    // Throw the custom CarIsDeadException.
                    CarIsDeadException ex = new CarIsDeadException(String.Format("{0} has overheated!", PetName), "You have a lead foot", DateTime.Now);
                    ex.HelpLink = "http://www.CarsRUs.com";
                    throw ex;
                }
                else
                {
                    Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
コード例 #6
0
ファイル: Car.cs プロジェクト: trollric/Csharp_chp3
 public void Accelerate(int delta)
 {
     if (delta < 0)
     {
         throw new ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
     }
     if (carIsDead)
     {
         Console.WriteLine("{0} is out of order due to an exploded engine...", PetName);
     }
     else
     {
         CurrSpeed += delta;
         if (CurrSpeed >= MaxSpeed)
         {
             CurrSpeed = 0;
             carIsDead = true;
             // Use the throw keyword to raise an exception.
             CarIsDeadException ex = new CarIsDeadException(string.Format("{0} has overheated!", PetName), "You have a lead foot", DateTime.Now);
             ex.HelpLink = "www.google.com";
             throw ex;
         }
         else
         {
             Console.WriteLine("=> CurrSpeed = {0}", CurrSpeed);
         }
     }
 }
コード例 #7
0
ファイル: Car.cs プロジェクト: burnakov/Troelsen
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new ArgumentOutOfRangeException("delta", "Speed must be greter then zero");
            }

            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order...", PetName);
            }
            else
            {
                CurrSpeed += delta;
                if (CurrSpeed > MaxSpeed)
                {
                    carIsDead = true;
                    CurrSpeed = 0;
                    CarIsDeadException cEx = new CarIsDeadException(String.Format("{0} has overheated", PetName), "Rules ignorance", DateTime.Now);
                    cEx.HelpLink = "www.google.com";
                    throw cEx;
                }
                else
                {
                    Console.WriteLine("=> Current speed is: {0}", CurrSpeed);
                }
            }
        }
コード例 #8
0
ファイル: Car.cs プロジェクト: Geronimobile/DotNetExamIntro
        public void Accelerate(int delta) {
            if (delta < 0) {
                throw new ArgumentOutOfRangeException("delta", "Speed must be grater than zero");
            }
            if (isCarDead) {
                Console.WriteLine("{0} is out of order", PetName);
            } else {
                CurrentSpeed += delta;
                if (CurrentSpeed > MaxSpeed) {
                    CurrentSpeed = 0;
                    isCarDead = true;
                    //Console.WriteLine("{0} has overheated", PetName);
                    //throw new Exception(string.Format("{0} has overheated", PetName));
                    CarIsDeadException ex = new CarIsDeadException(
                        string.Format("{0} has overheated", PetName),
                        "You have a lead foot", DateTime.Now);
                    ex.HelpLink = "http://www.Cars.com";


                    throw ex;

                } else {
                    Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
コード例 #9
0
        // See if Car has overheadred
        public void Accelerate(int delta)
        {
            // test for invalid argumnet before proceeding
            if (delta < 0)
            {
                throw new
                      ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
            }

            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order...", PetName);
            }
            else
            {
                CurrentSpeed += delta;
                if (CurrentSpeed > MaxSpeed)
                {
                    CurrentSpeed = 0;
                    carIsDead    = true;

                    // 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.google.co.uk";
                    throw ex;
                }
                else
                {
                    Console.WriteLine("=> CurrentSpeed = {0}", CurrentSpeed);
                }
            }
        }
        public override void Accelerate(int delta)
        {
            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order...", CarName);
            }
            else
            {
                CurrentSpeed += delta;

                if (CurrentSpeed >= maxSpeed)
                {
                    carIsDead = true;

                    // throw new ArgumentOutOfRangeException();

                    // We need to call the HelpLink property, thus we need
                    // to create a local variable before throwing the Exception object.

                    //Exception ex =
                    //  new Exception($"{CarName} has overheated! {CurrentSpeed} Over max speed of {MaxSpeed}");

                    CarIsDeadException ex =
                        new CarIsDeadException($"{CarName} has overheated! {CurrentSpeed} Over max speed of {maxSpeed}")
                    {
                        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.");

                    CurrentSpeed = 0;

                    ArgumentOutOfRangeException ex2 = new ArgumentOutOfRangeException("Too much acceleration!");
                    Exception ex3 = new Exception("Generic problem with acceleration");

                    // change this to ex1 or ex2 or ex3 to see how each exception is handled

                    throw ex3;
                }
                else
                {
                    Console.WriteLine("=> {0} CurrentSpeed = {1}, {2}", CarName, CurrentSpeed, carRadio);
                }
            }
        }
コード例 #11
0
        public void Accelerate(int delta)
        {
            if (carIsDead)
            {
                Console.WriteLine("{0} is out of order", PetName);
            }
            else
            {
                if (delta < 0)
                {
                    throw new ArgumentOutOfRangeException("Delta", "Speed must be greater than zero");
                }

                if (carIsDead)
                {
                    Console.WriteLine("{0} is out of order", PetName);
                }
                else
                {
                    CurrentSpeed += delta;
                    if (CurrentSpeed > maxSpeed)
                    {
                        //Console.WriteLine("{0} has overhiated ",PetName);
                        CurrentSpeed = 0;
                        carIsDead    = true;

                        CarIsDeadException ex = new CarIsDeadException($"{PetName} has overheated", "You have a lead foot", DateTime.Now);

                        //ex.HelpLink = "http://www.carsRUs.com";
                        //ex.Data.Add("Times stamp", $"The car exploded ad {DateTime.Now}");
                        //ex.Data.Add("Cause", "You have a lead foot.");

                        throw ex;
                        //throw new Exception($"{PetName} has overheated!");
                    }
                    else
                    {
                        Console.WriteLine("=> Current speed = {0} ", CurrentSpeed);
                    }
                }
            }
        }
コード例 #12
0
        // See if Car has overheated.
        // This time, throw an exception if the user speeds up beyond MaxSpeed.
        public void Accelerate(int delta)
        {
            if (delta < 0)
            {
                throw new
                      ArgumentOutOfRangeException("delta", "Speed must be greater than zero!");
            }


            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);
                }
            }
        }