so i am working on a program that gets input from the user for a triangle and should not take a negative number and display to the user that it is invalid. then needs to input the correct number. the problem is that my try and catch is not display. any suggestions is welcomed.

namespace Lab3
{   
   
    class TriangleClass : GeometricObject
    {    
        private double bottom = 1;
        private double height = 1;
        private double side = 1;
       /// <summary>
       /// default constructor
       /// </summary>
        public TriangleClass()
        {
        }
        /// <summary>
        /// Overload constructer that specifies values
        /// </summary>
        /// <param name="bottom"></param>
        /// <param name="height"></param>
        /// <param name="side"></param>
        /// <param name="color"></param>
        /// <param name="filled"></param>
        public TriangleClass(double bottom,double height,double side, string color, bool filled)
        {
            this.bottom = bottom;
            this.height = height;
            this.side = side;
        }

        /// <summary>
        /// Returns the value of bottom
        /// </summary>
        /// <returns></returns>
        public double getBottom()
        {
            return bottom;
        }

        /// <summary>
        /// sets the value of bottom
        /// </summary>
        /// <param name="bottom"></param>
        public void setBottom(double bottom)
        {
            try // tries for error
            {
                this.bottom = bottom;

                if (this.bottom < 0)
                {
                    throw new InvalidOperationException
                    ("must be positive");
                }
            }
            catch //catches and asks for new number
            {
                //Console.WriteLine(Exception ex);
                while (this.bottom < 0)
                {
                    Console.Clear();
                    Console.WriteLine("error: enter a new number");
                    this.bottom = Convert.ToDouble(Console.ReadLine());
                }
            }
        }

        /// <summary>
        /// Returns the value of height
        /// </summary>
        /// <returns></returns>
        public double getHeight()
        {
            return height;
        }

        /// <summary>
        /// Sets the value of height
        /// </summary>
        /// <param name="height"></param>
        public void setHeight(double height)
        {
            try // tries for error number for height
            {
                this.height = height;

                if (this.height < 0)
                {
                    throw new InvalidOperationException
                    ("height must be positive");
                }
            }
            catch// catches error and askes for new number
            {
                //Console.WriteLine(Exception ex);
                while (this.height < 0)
                {
                    Console.Clear();
                    Console.WriteLine("error: enter a new number");
                    this.height = Convert.ToDouble(Console.ReadLine());
                }
            }
        }

        /// <summary>
        /// Returns the value of side
        /// </summary>
        /// <returns></returns>
        public double getSide()
        {
            return side;
        }

        /// <summary>
        /// Sets the value of the side
        /// </summary>
        /// <param name="side"></param>
        public void setSide(double side)
        {
            try// tries for error on side
            {
                this.side = side;

                if (this.side < 0)
                {
                    throw new InvalidOperationException
                    ("must be positive");
                }
            }  
            catch (Exception e)
            //catches error and asks for new number
            {
                //Console.WriteLine(Exception ex);
                
                    Console.Clear();
                    MessageBox.Show(e.Message);
                    this.side = Convert.ToDouble(Console.ReadLine());
                
            }
        }

        /// <summary>
        /// Returns and calculates the area of the triangle
        /// </summary>
        /// <returns></returns>
        public double getArea()
        {
            return ((bottom * height)/2);
        }

        /// <summary>
        /// returns and calculates the perimeter of the triangle
        /// </summary>
        /// <returns></returns>
        public double getPerimeter()
        {
            return (bottom + height + side);
        }
    }
}

Show the code for how you're consuming the class.

A couple of items, though.

1) The program looks more like a Java program. Are you of a Java background (or perhaps is your teacher)? In C#, we use Pascal-casing to name methods. Basically, the first letter of each word is capitalized, as opposed to the camel-casing you're employing.

Also, you're using getter and setter methods. Look at using properties instead, such as the below:

private int height = 0;
public int Height 
{
    get { return height; }
    set { height = value; }
}

If you're using C# 3.0 (Visual Studion 2008), you can use auto-implemented properties so you can avoid the private instance field altogether.

public int Height { get; set; }

2) You're doing too much work in the catch clause and you're throwing an exception just to catch it in the same method, which isn't really a good practice. Exceptions are expensive, don't rely on them (a) for validation or (b) to control the flow of your program. Also, what if your work inside the catch clause throws an exception? What's going to catch that? Looping until you get a valid input is fine, but use if statements and methods like .TryParse (usable with ints, doubles, floats, etc.) to validate your inputs without relying on exception handling.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.