当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - user defined illegal exception

问题描述:

I have this program where I'm supposed to return illegal triangle exception if sum of any 2 sides are greater than one. Where am i supposed to put the

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

in here? I'm not sure where to put it. Also i would like to know if my way of writing the code is correct?

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

}

}

}

网友答案:

First of all.

  • Properties are public and internal fields should be private. Read this guide https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx?f=255&MSPPError=-2147217396)
  • You should first make an instance of your Triangle with the Triangle(double s1, double s2, double s3) constructor. In that constructor you can call the private CheckForIllegalTriangle() to check the side lengths. This will then throw your exception if there is a fault.
  • Make the default constructor private. So you dont have to worrie about calling the 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);
        }
    }
}
网友答案:

Zohar's answer is a good implementation of a mutable triangle with invariants.

However, there's a good argument for immutability here (ie, no setters), otherwise you end up with issues passing through invalid states to transition between valid ones:

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

The immutable version is much simpler to implement

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

(Note I've also swapped < for <=, and fixed the negative lengths checks)

网友答案:

Here is what I would do:
By keeping the fields private you ensure that any value assignment to them will be only by using the appropriate properties.
Note that the constructor is assigning the first and second values to the fields and the third to the property to check for illegal triangles.

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");
        }
    }
}
网友答案:

It's impossible that every side is greater than the 2 others, so I think your question is wrong. But here is what I think you're trying to achieve.

 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);
            }
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: