Exemple #1
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();
        }
Exemple #2
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();
        }
Exemple #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();
        }
        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();
        }