示例#1
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();
        }
示例#2
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();
        }
示例#3
0
        static void Main(string[] args)
        {
            Console.WriteLine("**** Handling multiple exceptions *****\n");
            Car myCar = new Car("Rusty", 90);
            try
            {
                myCar.Accelerate(90);
            }

            ////the order of these matter!  The most general exception must go last, or the first two
            ////will be met with compile-time errors.
            //catch (CarIsDeadException e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            //catch (ArgumentOutOfRangeException e)
            //{
            //    Console.WriteLine(e.Message);
            //}
            //catch (Exception e)
            //{
            //    Console.WriteLine(e.Message);
            //}

            catch {
                Console.WriteLine("Something bad happened...");
            }
            
            Console.ReadLine();
        }
示例#4
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);
            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();
        }
示例#6
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);
            }
            Console.ReadLine();
        }
        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);
            }
            // 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(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();
        }
示例#8
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();
        }
        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();
        }
示例#10
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);
            }
        }