用户定义的非法例外

时间:2015-05-25 16:25:07

标签: c# exception defined

我有这个程序,如果任何2边的总和大于1,我应该返回非法三角形异常。

我应该放在哪里
if(side1 + side2 < side3)
    throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
if(side1 + side3 < side2)
    throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
if(side3 + side2 < side1)
    throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");

在这里?我不知道该把它放在哪里。另外我想知道我编写代码的方式是否正确?

class Triangle
{
    public double side1, side2, side3;
    public Triangle() { }
    public Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;
    }
    public double Side1
    {
        get { return side1; }
        set {
            if (value<0)
            side1 = value;
        }
    }
    public double Side2
    {
        get { return side2; }
        set {
            if (value < 0)
            side2 = value;
        }
    }
    public double Side3
    {
        get { return side3; }
        set
        {
            if (value < 0)
            side3 = value;
        }
    }
}

class IllegalTriangleException : Exception
{
    public IllegalTriangleException() : base ("Sum of any 2 sides is not greater than the other") { }
    public IllegalTriangleException(string msg) : base("Sum of any 2 sides is not greater than the other" + msg) { }
    public IllegalTriangleException(string msg, Exception innerException) : base("Sum of any 2 sides is not greater than the other" + msg, innerException){ }
}

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Length of side: ");
            double side1 = Convert.ToDouble(Console.ReadLine());
            double side2 = Convert.ToDouble(Console.ReadLine());
            double side3 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Your triangle is puuuuurfect");
        }
        catch (IllegalTriangleException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

4 个答案:

答案 0 :(得分:2)

Zohar's answer是具有不变量的可变三角形的良好实现。

然而,这里有一个关于不变性的好论据(即没有设定者),否则你最终会遇到通过无效状态的问题,以便在有效状态之间转换:

Triangle a(3, 4, 5);
Triangle b(30, 40, 50);

// let's adjust triangle a to match b
try {
    a.Side1 = b.Side1;  // nope
    a.Side2 = b.Side2;
    a.Side3 = b.Side3;
}
catch(IllegalTriangleException e) {
    // darn, we accidentally went through an invalid state of (30, 4, 5)
}

不可变版本实现起来要简单得多

class ImmutableTriangle
{
    public ImmutableTriangle(double side1, double side2, double side3)
    {
        if(side1 + side2 <= side3 ||
           side2 + side3 <= side1 ||
           side3 + side1 <= side2)
            throw new IllegalTriangleException("Sum of any 2 sides not bigger than the other side");
        }
        if(side1 <= 0 || side2 <= 0 || side3 <= 0)
            throw new IllegalTriangleException("Sides must be positive");
        }
        Side1 = side1;
        Side2 = side2;
        Side3 = side3;
    }
    public double Side1 { get; private set; }
    public double Side2 { get; private set; }
    public double Side3 { get; private set; }
}

(注意我还为<交换了<=,并修正了负长度检查)

答案 1 :(得分:1)

这是我要做的:
通过将字段保密,您可以确保只为使用适当的属性赋值给它们 请注意,构造函数将第一个和第二个值分配给字段,第三个值分配给属性以检查非法三角形。

public class Triangle
{
    private double _side1, _side2, _side3;
    public Triangle() { }
    public Triangle(double side1, double side2, double side3)
    {
        _side1 = side1;
        _side2 = side2;
        Side3 = side3; // Note: this is calling the property
    }
    public double Side1
    {
        get { return _side1; }
        set {
            if (value > 0) 
            {
            CheckForIllegalTriangle(value, Side2, Side3);
            _side1 = value;
            }
        }
    }
    public double Side2
    {
        get { return _side2; }
        set {
            if (value > 0)
            {
            CheckForIllegalTriangle(Side1, value, Side3);
            _side2 = value;
            }
        }
    }
    public double Side3
    {
        get { return _side3; }
        set
        {
            if (value > 0)
            {
            CheckForIllegalTriangle(Side1, Side2, value);
            _side3 = value;
            }
        }
    }

    private void CheckForIllegalTriangle(double side1, double side2, double side3) {
        if((side1 + side2 < side3) || 
           (side1 + side3 < side2) || 
           (side3 + side2 < side1))
        {
           throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
        }
    }
}

答案 2 :(得分:0)

首先。

  • 属性是公共的,内部字段应该是私有的。阅读本指南https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx?f=255&MSPPError=-2147217396
  • 您应首先使用三角形(双s1,双s2,双s3)构造函数创建三角形的实例。在该构造函数中,您可以调用私有CheckForIllegalTriangle()来检查边长。如果出现故障,这将抛出您的异常。
  • 将默认构造函数设为私有。因此,您不必担心调用CheckForIllegalTriangle()。
class Triangle
{
    private double side1, side2, side3;

    private Triangle() { }

    public Triangle(double s1, double s2, double s3)
    {
        side1 = s1;
        side2 = s2;
        side3 = s3;

        CheckForIllegalTriangle();
    }

    public double Side1
    {
        get { return side1; }
        set
        {
            if (value < 0)
                side1 = value;
        }
    }

    public double Side2
    {
        get { return side2; }
        set
        {
            if (value < 0)
                side2 = value;
        }
    }

    public double Side3
    {
        get { return side3; }
        set
        {
            if (value < 0)
                side3 = value;
        }
    }

    public void CheckForIllegalTriangle()
    {
        if ((side1 + side2 < side3) ||
           (side1 + side3 < side2) ||
           (side3 + side2 < side1))
            throw new IllegalTriangleException("Sum of any 2 sides not bigger than the other side");
    }
}


class IllegalTriangleException : Exception
{
    public IllegalTriangleException() : base("Sum of any 2 sides is not greater than the other") { }
    public IllegalTriangleException(string msg) : base("Sum of any 2 sides is not greater than the other" + msg) { }
    public IllegalTriangleException(string msg, Exception innerException) : base("Sum of any 2 sides is not greater than the other" + msg, innerException) { }
}

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Console.WriteLine("Length of side 1: ");
            double side1 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Length of side 2: ");
            double side2 = Convert.ToDouble(Console.ReadLine());
            Console.WriteLine("Length of side 3: ");
            double side3 = Convert.ToDouble(Console.ReadLine());
            Triangle t1 = new Triangle(side1, side2, side3);

            Console.WriteLine("Your triangle is puuuuurfect");
        }
        catch (IllegalTriangleException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

答案 3 :(得分:0)

每一方都不可能超过另外两方,所以我认为你的问题是错误的。但这就是我认为你想要实现的目标。

 class Triangle
    {
        public double side1, side2, side3;
        public Triangle() { }
        public Triangle(double s1, double s2, double s3)
        {
            side1 = s1;
            side2 = s2;
            side3 = s3;
            if(side1 + side2 < side3)
               throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
            if(side1 + side3 < side2)
               throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
            if(side3 + side2 < side1)
               throw new illegalTriangleException("Sum of any 2 sides not bigger than the other side");
        }
        public double Side1
        {
            get { return side1; }
            set {
                if (value>0)
                side1 = value;
            }
        }
        public double Side2
        {
            get { return side2; }
            set {
                if (value > 0)
                side2 = value;
            }
        }
        public double Side3
        {
            get { return side3; }
            set
            {
                if (value > 0)
                side3 = value;
            }
        }
    }

    class IllegalTriangleException : Exception
    {
        public IllegalTriangleException(string msg) : base(msg) { }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Length of side: ");
                double side1 = Convert.ToDouble(Console.ReadLine());
                double side2 = Convert.ToDouble(Console.ReadLine());
                double side3 = Convert.ToDouble(Console.ReadLine());
                Triangle t = new Triangle(side1, side2, side3);
                Console.WriteLine("Your triangle is puuuuurfect");
            }
            catch (IllegalTriangleException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }