コード例 #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)
            {
                try
                {
                    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();
        }
コード例 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("***** Fun with Custom Exceptions *****\n");
            Car myCar = new Car("Rusty", 90);

            try
            {
                myCar.Accelerate(50);
            }
            catch (CarIsDeadException exception)
            {
                Console.WriteLine(exception.Message);
                Console.WriteLine(exception.ErrorTimeStamp);
                Console.WriteLine(exception.CauseOfError);
            }

            Console.ReadLine();
        }
コード例 #3
0
        static void Main()
        {
            Console.WriteLine("***** Simple Exception Example *****\n");
            Console.WriteLine("=> Creating a car and stepping on it!");

            Car myCar = new Car("Zippy", 20);
            myCar.CrankTunes(true);

            try
            {
                for (int i = 0; i < 10; i++)
                    myCar.Accelerate(10);
            }
            catch (Exception e)
            {
                Console.WriteLine("\n*** Error! ***");
                Console.WriteLine("Method: {0}", e.TargetSite);
                Console.WriteLine("Class defining member: {0}", e.TargetSite.DeclaringType);
                Console.WriteLine("Member type: {0}", e.TargetSite.MemberType);
                Console.WriteLine("Message: {0}", e.Message);
                Console.WriteLine("Source: {0}", e.Source);

                Console.WriteLine("Stack: {0}", e.StackTrace);

                Console.WriteLine("Help link: {0}", e.HelpLink);

                Console.WriteLine("\n-> Custom data:");

                if (e.Data != null)
                {
                    foreach (DictionaryEntry de in e.Data)
                        Console.WriteLine("-> {0}: {1}", de.Key, de.Value);
                }
            }

            Console.WriteLine("\n***** Out of exception logic *****");

            Console.ReadLine();
        }