Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            myCar.CrankTunes(true);
            myCar.CurrentSpeed = 0;
            try
            {
                //Arg вызовет исключение выхода за пределы диапазона
                myCar.Accelerate(10);
            }

            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                //Этот код будет выполняться всегда, возникало исключение или нет
                myCar.CrankTunes(false);
            }

            Console.ReadLine();
        }
        // This code compiles just fine.
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");

            Car myCar = new Car("Rusty", 90);

            myCar.CrankTunes(true);

            #region Try / Catch logic.
            try
            {
                // Trip Arg out of range exception.
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                try
                {
                    FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
                    // Assume we write stack trace to file...
                }
                catch (Exception e2)
                {
                    // Throw an exception that records the new exception,
                    // as well as the message of the first exception.
                    throw new CarIsDeadException(e.Message, e2);
                }
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
            #endregion

            Console.ReadLine();
        }
Ejemplo n.º 3
0
        // This code compiles just fine.
        static void Main( string[] args )
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);
            try
            {
                // Trigger an argument out of range exception.
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }

            Console.ReadLine();
        }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            Console.WriteLine("****** Handlong Multiple Exceptions ******\n");
            Car myCar = new Car("Rusty", 90);

            try
            {
                // Trip arg out of range exception.
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                // This line will only print if the "when" keyword evaluates true
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // Safeguard catch
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            // This piece of code will always occur Exception or not
            finally
            {
                myCar.CrankTunes(false);
            }
            Console.ReadLine();
        }
Ejemplo n.º 5
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Handling Multiple Exceptions ****\n");
            Car myCar = new Car("Rusty", 90);
            try
            {
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                myCar.CrankTunes(false);
            }

            Console.ReadLine();
        }
Ejemplo n.º 6
0
        private static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            var myCar = new Car("Rusty", 90);

            try
            {
                myCar.Accelerate(-10);
            }
            //catch (CarIsDeadException e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            //catch (ArgumentOutOfRangeException e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            catch (CarIsDeadException e) when
                (e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                Console.WriteLine("Something bad happenned...");
            }
            finally
            {
                myCar.CrankTunes(false);
            }

            Console.ReadLine();
        }
Ejemplo n.º 7
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            #region Try/catch logic
            try
            {
                // Trip Arg out of range exception.
                myCar.Accelerate(1000);
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
            #endregion

            Console.ReadLine();
        }
Ejemplo n.º 8
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Handing Muliple Exceptions *****\n");

            Car myCar = new Car("Rusty", 90);

            try
            {
                //触发超出范围异常
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                myCar.CrankTunes(false);
            }

            Console.ReadLine();
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions");
            Car mycar = new Car("rusty", 90);

            try
            {
                mycar.Accelerate(2000);
            }
            catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                mycar.CrankTunes(false);
            }
            Console.Read();
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car(90, "Rusty");

            myCar.CrankTunes(true);
            try
            {
                try
                {
                    myCar.Accelerate(150);
                }
                catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
                {
                    try
                    {
                        FileStream fs = File.Open(@"carErrors.txt", FileMode.Open);
                    }
                    catch (Exception e2)
                    {
                        throw new CarIsDeadException(e.CauseOfError, e.ErrorTimeStamp, e.Message, e2);
                    }
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                myCar.CrankTunes(false);
                Console.WriteLine("This code block executed anyway>>>>>>>>>>>>>>>>>>>>>>>>");
            }

            Console.ReadLine();
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            try
            {
                // Trip Arg out of range exception.
                //myCar.Accelerate(-10);
                //myCar.Accelerate(100);
            }
            //This causes a compile error
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            catch (CarIsDeadException e) //when (e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                Console.WriteLine(e.Message);
                try
                {
                    FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
                }
                catch (Exception e2)
                {
                    // This causes a compile error - InnerException is read only
                    // e.InnerException = e2;
                    // Throw an exception that records the new exception,
                    // as well as the message of the first exception.
                    throw new CarIsDeadException(e.CauseOfError, e.ErrorTimeStamp, e.Message, e2);
                }
                //Rethrow the exception to the caller
                throw;
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
            Console.ReadLine();
        }
Ejemplo n.º 12
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Multiple exceptions ****");
            Car myCar = new Car("BMW", 70);

            myCar.CrankTunes(true);
            try
            {
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                try
                {
                    Console.WriteLine(e.Message);
                    FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
                }
                catch (Exception e2)
                {
                    throw new CarIsDeadException(e.Message, e2);
                }
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                myCar.CrankTunes(false);
            }
            Console.ReadLine();
        }
Ejemplo n.º 13
0
        static void Main(string[] args) {
            Car myCar = new Car("Zippy", 20);

            try {
                myCar.Accelerate(-10);

            } catch (CarIsDeadException e) {
                Console.WriteLine("Message: {0}", e.Message);
           // } catch (ArgumentException e) {
            //    Console.WriteLine("Message: {0}", e.Message);
            } finally {
                myCar.CrankTunes(false);
            }


            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90, 1);

            Console.WriteLine($"Auto initialized: {myCar}");

            #region Try/catch logic
            try
            {
                // Trip Arg out of range exception.
                myCar.Accelerate(1000);
            }
            // Just for fun...
            catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                Console.WriteLine("Catching car is dead!");
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine("Catching ArgumentOutOfRangeException " + e.InnerException);

                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                Console.WriteLine("Finally exception");
                myCar.CrankTunes(true);
            }
            #endregion

            Console.ReadLine();
        }
Ejemplo n.º 15
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);
            try
            {
                // Trip Arg out of range exception.
                myCar.Accelerate(-10);
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
                try
                {
                    FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
                }
                catch (Exception e2)
                {
                    // Throw an exception that records the new exception,
                    // as well as the message of the firest excepton.
                    throw new CarIsDeadException(e.Message, e2);
                }
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }

            Console.ReadKey();
        }
Ejemplo n.º 16
0
        // This code compiles just fine.
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            try
            {
                // Trigger an argument out of range exception.
                myCar.Accelerate(-10);
                FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
            }

            catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                // This new line will only print if the when clause evaluates to true.
                Console.WriteLine("Catching car is dead!");

                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e2)
            {
                // Throw an exception that records the new exception,
                // as well as the message of the first exception.
                throw new CarIsDeadException(e.Message, e2);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
            Console.ReadLine();
        }
Ejemplo n.º 17
0
        static void Main(string[] args)
        {
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-US");
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            #region Try/catch logic
            try
            {
                // Trip Arg out of range exception.
                //myCar.Accelerate(-5);
                myCar.Accelerate(1000);
                //int a = 2 - 2;
                //int b = 1 / a;
            }
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.ToString());
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException.
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
            #endregion

            Console.ReadLine();
        }
Ejemplo n.º 18
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");

            Car myCar = new Car("Rusty", 90);

            myCar.CrankTunes(true);
            try
            {
                //  Trip Arg out of range exception.
                //myCar.Accelerate(-10);
                myCar.Accelerate(20);
            }
            //  Processes first approprate catch so
            //  CarIsDeadException and ArgumentOutOfRangeException
            //  are unreachealbe wehn Exception can handle anything
            //  derived from System.Exception.
            //catch(Exception e)
            //{
            //    //  Process all other exceptions?
            //    Console.WriteLine(e.Message);
            //}
            catch (CarIsDeadException e) when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
            {
                Console.WriteLine("Catching car is dead!");
                Console.WriteLine(e.Message);
                //  Do any partial processing of this error and
                //  pass the buck...Rethrow.
                //throw;
                //try
                //{
                //    //  Attempt to open a file name carError.txt on the C drive.
                //    FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
                //}
                //catch (Exception e2)
                //{
                //    //  Throw an exception that records the new exception,
                //    //  as well as the message of the first exception.
                //    throw new CarIsDeadException(e.Message, e2);
                //}
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            //  Always catch most derived(specific) exceptions first
            //  to least derived(general) last.
            //catch (Exception e)
            //{
            //    //  Process all other exceptions?
            //    Console.WriteLine(e.Message);
            //}
            //  A general catch.
            catch
            {
                Console.WriteLine("Something bad happened...");
            }
            finally
            {
                //  This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }
        }
        static void Main(string[] args)
        {
            Console.WriteLine("***** Handling Multiple Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            try
            {
                // Trip Arg out of range exception.
                myCar.Accelerate(-10);
            }
            //catch (Exception e)
            //{
            //    // Process all other exceptions?
            //    Console.WriteLine(e.Message);
            //}
            catch (CarIsDeadException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            // This will catch any other exception
            // beyond CarIsDeadException or
            // ArgumentOutOfRangeException
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            // A generic catch
            myCar = new Car("Rusty", 90);
            try
            {
                myCar.Accelerate(90);
            }
            catch
            {
                Console.WriteLine("Something bad happened...");
            }

            // Passing the buck
            //myCar = new Car("Rusty", 90);
            //try
            //{
            //    myCar.Accelerate(90);
            //}
            //catch (CarIsDeadException e)
            //{
            //    // Do any partial processing of this error and pass the buck.
            //    throw;
            //}

            // Inner exception
            //myCar = new Car("Rusty", 90);
            //try
            //{
            //    myCar.Accelerate(90);
            //}
            //catch (CarIsDeadException e)
            //{
            //    try
            //    {
            //        FileStream fs = File.Open(@"C:\carErrors.txt", FileMode.Open);
            //    }
            //    catch (Exception e2)
            //    {
            //        // Throw an exception that records the new exception,
            //        // as well as the message of the first exception.
            //        throw new CarIsDeadException(e.Message, e2);
            //    }
            //}

            myCar = new Car("Rusty", 90);
            myCar.CrankTunes(true);
            try
            {
                myCar.Accelerate(90);
            }
            catch (CarIsDeadException e)
                when(e.ErrorTimeStamp.DayOfWeek != DayOfWeek.Friday)
                {
                    // This new line will only print if the when clause evaluates to true.
                    Console.WriteLine("Catching car is dead!");

                    Console.WriteLine(e.Message);
                }
            catch (ArgumentOutOfRangeException e)
            {
                Console.WriteLine(e.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
            finally
            {
                // This will always occur. Exception or not.
                myCar.CrankTunes(false);
            }

            myCar = new Car("Rusty", 90);
            myCar.Accelerate(2000);

            Console.ReadLine();
        }