StackTrace

Filed Under (kód) by nameless on 25-07-2009

StackTrace tulajdonság:

using System;
 
namespace Program
{
    class Radio
    {
        public void TurnOn(bool on)
        {
            if (on)
                Console.WriteLine("Jamming...");
            else
                Console.WriteLine("Quite time...");
        }
    }
 
    class Car
    {
        //max sebesség konstansa
        public const int MaxSpeed = 100;
 
        //Belső állapot adatok
        private int currSpeed;
        private string petName;
 
        //az autó még mindig működőképes?
        private bool CarIsDead;
 
        //Az autónak van egy rádiója
        private Radio theMusicBox = new Radio();
 
        //Konstruktorok
        public Car() { }
        public Car(string name, int currSp)
        {
            currSpeed = currSp;
            petName = name;
        }
 
        //Rádió be?
        public void CrankTunes(bool state)
        {
            theMusicBox.TurnOn(state);
        }
 
        //Kivételt jelez, hogyha a felhasználó a MaxSpeed értéke fölé gyorsítja za autót.
        public void Accelerate(int delta)
        {
            if (CarIsDead)
                Console.WriteLine("{0} is out of order...", petName);
            else
            {
                currSpeed += delta;
                if (currSpeed > MaxSpeed)
                {
                    CarIsDead = true;
                    currSpeed = 0;
 
                    //Kivétel kiváltása a throw segítségével
                    throw new Exception(string.Format("{0} has overheated!",petName));
                }
                else
                    Console.WriteLine("=> CurrSpeed = {0}", currSpeed);
            }
        }
 
 
    }
 
    class MainClass
    {
        public static void Main()
        {
            Console.WriteLine("*****Simple Exception Example*****");
            Console.WriteLine("=> Creating a car and steppong on it!");
            var myCar = new Car("David", 20); //zippy a neve és jelenleg 20-szal megyünk
            myCar.CrankTunes(true); //rádió bekapcsolva
 
            try
            {
                for (int i = 0; i < 10; i++)
                    myCar.Accelerate(10); //10-zel nő a sebesség addig, amíg elnem éri a 100-at, utána felrobban
            }
            catch (Exception ex)
            {
                Console.WriteLine("*** Error! ***");
 
                //************************<StackTrace>******************
                Console.WriteLine("\n\nStack: {0}\n\n", ex.StackTrace); //megmutatja a hiba pontos helyét!
                //************************</StackTace>*****************
 
                Console.WriteLine("Method: {0}", ex.TargetSite);
                Console.WriteLine("Class defining member: {0}",ex.TargetSite.DeclaringType);
                Console.WriteLine("Member type: {0}", ex.TargetSite.MemberType);
                Console.WriteLine("Message: {0}", ex.Message);
                Console.WriteLine("Source: {0}", ex.Source);
            }
 
            //A hibát kezeltük, a program FOLYTATÓDIK
            Console.WriteLine("\n***** Out of Exception logic *****");
            Console.ReadKey();
        }
    }
 
}

Post a comment