簡體   English   中英

用戶定義的非法異常

[英]user defined illegal exception

我有這個程序,如果任意兩個邊的總和大於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);
        }
    }
}

Zohar的答案是具有不變性的可變三角形的良好實現。

但是,這里有一個關於不變性的很好的論點(即沒有設置器),否則您將面臨通過無效狀態在有效狀態之間轉換的問題:

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

(請注意,我還用<替換了<= ,並修復了負數長度檢查)

這是我會做的:
通過將字段設為私有,可以確保僅通過使用適當的屬性來為其分配任何值。
請注意,構造函數將第一個和第二個值分配給字段,將第三個值分配給屬性以檢查非法三角形。

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

首先。

  • 屬性是公共的,內部字段應該是私有的。 閱讀本指南https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx?f=255&MSPPError=-2147217396
  • 您首先應該使用Triangle(double s1,double s2,double s3)構造函數創建Triangle的實例。 在該構造函數中,您可以調用私有的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);
        }
    }
}

每一方都不可能大於另一方,所以我認為您的問題是錯誤的。 但是,這就是我想您要實現的目標。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM