Using exception handling in my code.

I would like to create some sort of exception handling in my code for my choices, but I am having issues what I have I would have thought worked but I am getting these errors:

Program.cs(193,31): error CS0155: The type caught or thrown must be derived from System.Exception
Program.cs(196,24): error CS0155: The type caught or thrown must be derived from System.Exception
Program.cs(196,17): error CS0136: A local variable named 'choice' cannot be declared in this scope because it would give a different meaning to 'choice', which is already used in a 'parent or current' scope to denote something else

Here is my code please tell me what is wrong with it:


public class Character
    {
        private string name;
        private string answer;
        private int life;
        private bool mHeal = false;
        private Random randomMoney = new Random();
 
        public int RandomMoney
        {
            get
            {
                return randomMoney.Next(100);
            }
        }
        public bool Heal
        {
            get
            {
                return mHeal;
            }
            set
            {
                mHeal = value;
            }
        }
        public string Name
        {
            get
            {
                return (this.name);
            }
            set
            {
                this.name = value;
            }
        }
        public int Life
        {
            get
            {
                return (this.life);
            }
            set
            {
                this.life = value;
            }
        }
        public virtual void input()
        {
            Console.Write("Enter name: ");
            this.Name = Console.ReadLine();
 
            Console.Write("Enter life points: ");
            this.Life = int.Parse(Console.ReadLine());
 
            Console.Write("Can this character Heal (true or false): ");
            answer = Console.ReadLine();
            if (answer.ToUpper() == "TRUE")
            {
                this.Heal = true;
            }
            else
                this.Heal = false;
            
        }
        public virtual int calcPoints()
        {
            return Life;
        }
    }
    public class Warrior : Character
    {
        private int strength;
 
        public int Strength
        {
            get
            {
                return (this.strength);
            }
            set
            {
                this.strength = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter strength: ");
            this.Strength = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Strength;
        }
    }
    public class Mage : Character
    {
        private int intel;
 
        public int Intel
        {
            get
            {
                return (this.intel);
            }
            set
            {
                this.intel = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter intelligence: ");
            this.Intel = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Intel;
        }
    }
    public class Stalker : Character
    {
        private int agility;
 
        public int Agility
        {
            get
            {
                return (this.agility);
            }
            set
            {
                this.agility = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter agility: ");
            this.Agility = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Agility;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Character character = new Character();
            Warrior warrior = new Warrior();
            Mage mage = new Mage();
            Stalker stalker = new Stalker();
 
            string choice;
            bool done = false;
 
            character.input();
            do
            {
                try
                {
                    Console.Write("Pick a character class W - Warrior, M - Mage, S - Stalker: ");
                    choice = Console.ReadLine();
                    if (choice.ToUpper() == "W")
                    {
                        warrior.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nStrength {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, warrior.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "M")
                    {
                        mage.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nIntelligence: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, mage.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "S")
                    {
                        stalker.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nAgility: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, stalker.calcPoints()));
                        done = true;
                    }
                    else
                    {
                        throw choice;
                    }
                }
                catch (string choice)
                {
                    Console.Write("Error detected.... {0}", choice);
                    Console.Write(" is a correct option!");
                    done = false;
                }
            } while (!done);
        }
    }

Open in new window

jschmuffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

clockwatcherCommented:
You're redeclaring choice and need to throw an object which derives from Exception.  Try changing it to this:

       static void Main(string[] args)
        {
            Character character = new Character();
            Warrior warrior = new Warrior();
            Mage mage = new Mage();
            Stalker stalker = new Stalker();

            string choice;
            bool done = false;

            character.input();
            do
            {
                try
                {
                    Console.Write("Pick a character class W - Warrior, M - Mage, S - Stalker: ");
                    choice = Console.ReadLine();
                    if (choice.ToUpper() == "W")
                    {
                        warrior.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nStrength {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, warrior.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "M")
                    {
                        mage.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nIntelligence: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, mage.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "S")
                    {
                        stalker.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nAgility: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, stalker.calcPoints()));
                        done = true;
                    }
                    else
                    {
                        throw new Exception(choice);
                    }
                }
                catch (Exception choiceException)
                {
                    Console.Write("Error detected.... {0}", choiceException.Message);
                    Console.Write(" is a correct option!");
                    done = false;
                }
            } while (!done);
        }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JimBrandleyCommented:
Your problem is that throw and catch need Exception objects. The code below will fix that.

Jim
public class Character
    {
        private string name;
        private string answer;
        private int life;
        private bool mHeal = false;
        private Random randomMoney = new Random();
 
        public int RandomMoney
        {
            get
            {
                return randomMoney.Next(100);
            }
        }
        public bool Heal
        {
            get
            {
                return mHeal;
            }
            set
            {
                mHeal = value;
            }
        }
        public string Name
        {
            get
            {
                return (this.name);
            }
            set
            {
                this.name = value;
            }
        }
        public int Life
        {
            get
            {
                return (this.life);
            }
            set
            {
                this.life = value;
            }
        }
        public virtual void input()
        {
            Console.Write("Enter name: ");
            this.Name = Console.ReadLine();
 
            Console.Write("Enter life points: ");
            this.Life = int.Parse(Console.ReadLine());
 
            Console.Write("Can this character Heal (true or false): ");
            answer = Console.ReadLine();
            if (answer.ToUpper() == "TRUE")
            {
                this.Heal = true;
            }
            else
                this.Heal = false;
            
        }
        public virtual int calcPoints()
        {
            return Life;
        }
    }
    public class Warrior : Character
    {
        private int strength;
 
        public int Strength
        {
            get
            {
                return (this.strength);
            }
            set
            {
                this.strength = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter strength: ");
            this.Strength = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Strength;
        }
    }
    public class Mage : Character
    {
        private int intel;
 
        public int Intel
        {
            get
            {
                return (this.intel);
            }
            set
            {
                this.intel = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter intelligence: ");
            this.Intel = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Intel;
        }
    }
    public class Stalker : Character
    {
        private int agility;
 
        public int Agility
        {
            get
            {
                return (this.agility);
            }
            set
            {
                this.agility = value;
            }
        }
        public override void input()
        {
            Console.Write("Enter agility: ");
            this.Agility = int.Parse(Console.ReadLine());
        }
        public override int calcPoints()
        {
            return Agility;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Character character = new Character();
            Warrior warrior = new Warrior();
            Mage mage = new Mage();
            Stalker stalker = new Stalker();
 
            string choice;
            bool done = false;
 
            character.input();
            do
            {
                try
                {
                    Console.Write("Pick a character class W - Warrior, M - Mage, S - Stalker: ");
                    choice = Console.ReadLine();
                    if (choice.ToUpper() == "W")
                    {
                        warrior.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nStrength {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, warrior.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "M")
                    {
                        mage.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nIntelligence: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, mage.calcPoints()));
                        done = true;
                    }
                    if (choice.ToUpper() == "S")
                    {
                        stalker.input();
                        MessageBox.Show(string.Format("Character Name: {0}\nLife: {1}\nHealer: {2}\nMoney: {3}\nAgility: {4}", character.Name, character.calcPoints(), character.Heal, character.RandomMoney, stalker.calcPoints()));
                        done = true;
                    }
                    else
                    {
                        throw Exception(choice);
                    }
                }
                catch (Exception e)
                {
                    Console.Write("Error detected.... {0}", e.Message);
                    Console.Write(" is not a correct option!");
                    done = false;
                }
            } while (!done);
        }
    }

Open in new window

0
mokuleCommented:
You should
  throw new Exception(choice);

and

catch( Exception ex)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.