Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户定义的非法异常_C#_Exception_Defined - Fatal编程技术网

C# 用户定义的非法异常

C# 用户定义的非法异常,c#,exception,defined,C#,Exception,Defined,我有一个程序,如果任意两条边的和大于一,我应该返回非法三角形例外。我该把钥匙放在哪里 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 sid

我有一个程序,如果任意两条边的和大于一,我应该返回非法三角形例外。我该把钥匙放在哪里

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");
if(第1侧+第2侧<第3侧)
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
如果(第1侧+第3侧<第2侧)
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
如果(第3侧+第2侧<第1侧)
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
在这里?我不知道该把它放在哪里。我也想知道我写代码的方式是否正确

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);
        }
    }
}
类三角形
{
公共双边1、双边2、双边3;
公共三角形(){}
公共三角形(双s1、双s2、双s3)
{
侧1=s1;
侧2=s2;
侧3=s3;
}
公共双边关系1
{
获取{return side1;}
设置{

如果(value我会这样做:
通过保持字段的私有性,您可以确保仅通过使用适当的属性来为字段赋值。
请注意,构造函数将第一个和第二个值分配给字段,第三个值分配给属性,以检查非法三角形

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");
        }
    }
}
公共类三角形
{
私人双人房1、2、3;
公共三角形(){}
公共三角形(双面1、双面2、双面3)
{
_侧1=侧1;
_side2=side2;
Side3=Side3;//注意:这是调用属性
}
公共双边关系1
{
获取{return\u side1;}
设置{
如果(值>0)
{
检查非法三角形(值,边2,边3);
_side1=数值;
}
}
}
公共双边2
{
获取{return\u side2;}
设置{
如果(值>0)
{
检查非法三角形(边1,值,边3);
_side2=值;
}
}
}
公共双边3
{
获取{return\u side3;}
设置
{
如果(值>0)
{
检查非法三角形(边1,边2,值);
_side3=值;
}
}
}
非法三角形的专用无效检查(双面1、双面2、双面3){
如果((第1边+第2边<第3边)|
(侧1+侧3<侧2)|
(侧3+侧2<侧1))
{
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
}
}
}
首先

  • 属性是公共的,内部字段应该是私有的。请阅读本指南)
  • 您应该首先使用三角形(双s1、双s2、双s3)构造函数创建三角形的实例。在该构造函数中,您可以调用私有CheckForIllegalTriangle()来检查边长。如果有错误,这将引发异常
  • 将默认构造函数设为私有。这样您就不必担心调用CheckForIllegalTriangle()
类三角形
{
私人双边1,双边2,双边3;
私有三角形(){}
公共三角形(双s1、双s2、双s3)
{
侧1=s1;
侧2=s2;
侧3=s3;
CheckForIllegalTriangle();
}
公共双边关系1
{
获取{return side1;}
设置
{
如果(值<0)
side1=数值;
}
}
公共双边2
{
获取{return side2;}
设置
{
如果(值<0)
side2=值;
}
}
公共双边3
{
获取{return side3;}
设置
{
如果(值<0)
side3=值;
}
}
公共无效CheckForIllegalTriangle()
{
如果((第1侧+第2侧<第3侧)||
(第1侧+第3侧<第2侧)||
(侧3+侧2<侧1))
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
}
}
类IllegalTriangleException:异常
{
public-IllegalTriangleException():base(“任意两条边的总和不大于另一条”){}
public-IllegalTriangleException(string-msg):base(“任意两个边的总和不大于另一个”+msg){}
public IllegalTriangleException(string msg,Exception innerException):base(“任意两个边的总和不大于另一个”+msg,innerException){}
}
班级计划
{
静态void Main(字符串[]参数)
{
尝试
{
控制台。写入线(“侧1的长度:”);
双面1=Convert.ToDouble(Console.ReadLine());
控制台。写入线(“第2面长度:”);
双面2=Convert.ToDouble(Console.ReadLine());
控制台。写入线(“侧3的长度:”);
双面3=Convert.ToDouble(Console.ReadLine());
三角形t1=新三角形(边1、边2、边3);
WriteLine(“你的三角形很完美”);
}
捕获(非法三角例外)
{
控制台写入线(例如消息);
}
}
}

不可能每一方都比另外两方更强大,所以我认为你的问题是错误的。但以下是我认为你试图实现的目标

 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);
            }
        }
    }
类三角形
{
公共双边1、双边2、双边3;
公共三角形(){}
公共三角形(双s1、双s2、双s3)
{
侧1=s1;
侧2=s2;
侧3=s3;
如果(第1侧+第2侧<第3侧)
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
如果(第1侧+第3侧<第2侧)
抛出新的非法三角形例外(“任意两方之和不大于另一方”);
如果(第3侧+第2侧<第1侧)
抛出新的非法三角形异常
 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);
            }
        }
    }
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; }
}