class MyException : ApplicationException
{
public MyException(string Message): base(Message)
{Console.WriteLine("自定义的异常");}
}
//定义三角形类
class Triangle
{
public double a = 0, b = 0, c = 0;//三角形三边长
public double primeter = 0;//三角形周长
public double area = 0;//三角形面积
//三角形构造函数初始化三边,并对
public Triangle(double a, double b, double c)
{
if (a <= 0 || b <= 0 || c <= 0)
throw new ArgumentOutOfRangeException("边长不能为负数");
if ((a + b <= c) || (b + c <= a) || (c + a <= b))
throw new MyException ("三角形两边之和不能小于第三边");
this.a = a;
this.b = b;
this.c = c;
this.primeter = a + b + c;
}}
class Program
{
static void Main(string[] args)
{
double ma, mb, mc;
double l=0;//周长
Console.WriteLine("请输入三角形的三条边:");
try
{
ma = Convert.ToDouble(Console.ReadLine());
mb = Convert.ToDouble(Console.ReadLine());
mc = Convert.ToDouble(Console.ReadLine());
Triangle t = new Triangle(ma, mb, mc);
l = t.primeter;
}
catch (FormatException e)
{//派生程度最高的异常在最前 Console.WriteLine(e.Message); }
catch (ArgumentOutOfRangeException e)
{//参数超过有效值范围异常 Console.WriteLine(e.Message);}
catch (MyException e)
{//自定义异常 Console.WriteLine(e.Message);}