如何在C#中停止工作构造函数?

Imu*_*ugi 0 c#

这是典型的课程.

class Round
{
    private int radius;

    private bool Validate(int radius) 
    {

            if (radius <= 0)
            {
            return false;
            }
            else
        {
            return true;
        }

    }

    public int X { get; set;}
    public int Y { get; set;}

    public int Radius
    {
        get
        {
            return radius;
        }

        set
        {
            try
            {   
                if(!Validate(value))
                {
                    throw new ArgumentException();
                }
            radius = value;
            }
            catch (ArgumentException)
            {

                Console.WriteLine("ooops... something went wrong.");
                return;
            }

        }
    }

    public Round (int X, int Y, int radius)
    {

        try
        {
            if (!Validate(radius))
            {
                throw new ArgumentException();
            }
        this.X = X;
        this.Y = Y;
        this.radius = radius;
        }
        catch (Exception)
        {

            Console.WriteLine("ooops... something went wrong.");
        }
    }

    public double GetPerimeter()
    {
        return Math.PI * radius * 2;
    }

}

class Program
{
    static void Main(string[] args)
    {
        Round r = new Round(0,0,0);
        Console.WriteLine(r.GetPerimeter());
    }
}
Run Code Online (Sandbox Code Playgroud)

如您所见,我向构造函数发送了不正确的值,该值将值发送给验证程序.我无法理解我应该怎么做才能使构造函数停止创建对象???

Thi*_*ilo 5

停止构造函数的方法是抛出异常.

你这样做,但是你也抓住了这个例外.这样,构造函数将继续.别抓住它.