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);
}
}
}