繁体   English   中英

在十进制方法C#中尝试/捕获

[英]try/catch within decimal method c#

首先,这是我正在从事的一项工作,但这不是必需的,只是我想使用的一项。 我试图将try / catch语句与十进制方法一起使用。

    private decimal NewCar()
    {
        decimal newCar = 0.00m;

        try
        {
            newCar = decimal.Parse(vehicleTextBox.Text);
        }
        catch (FormatException)
        {
            MessageBox.Show("Invalid entry (Format Exception). \n" + "Please enter a valid decimal number.");
            Keyboard.Focus(vehicleTextBox);
            vehicleTextBox.SelectAll();
            return;
        }
        catch (Exception)
        {
            MessageBox.Show("Invalid entry (General Exception). \n" + "Please enter a valid decimal number.");
            Keyboard.Focus(vehicleTextBox);
            vehicleTextBox.SelectAll();
            return;
        }

        if (newCar < 0)
        {
            MessageBox.Show("Invalid entry (Negative Value). \n" + "Please enter a valid decimal number.");
            Keyboard.Focus(vehicleTextBox);
            vehicleTextBox.SelectAll();
            return;
        }

        return newCar;
    }

我收到一条错误消息,说“需要一个可转换为'decimal'的类型的对象”,但是如果我在其中放置newCar变量,或在try / catch中删除return语句,则会收到太多消息框,而它却没有不要停止计算。 我需要将其用作其他计算中使用的十进制方法。

任何帮助表示赞赏。

您已经说过您的方法将返回小数。 因此,您必须返回一个小数。 您不能只写return ,因为那表明您什么也不返回。

现在,您很可能应该将错误处理移出该方法,并仅允许异常冒泡。 由于您的方法正在解析汽车价值(顺便说一句,您的方法名称应表明, NewCar指示其正在创建新的汽车对象),因此它不必与用户进行交互。 唯一的目的应该是解析文本并给您十进制。

因此,您有两种选择:

  1. 您的方法返回错误代码(例如-1),以便您可以返回合适的值。 另一种选择是创建一个包装器类,该包装器类存储“成功”标志,数据和可能的异常。
  2. 让异常简单地冒出来。

为了增加Rob的正确和出色的答案,我还建议在捕获异常时使用TryParse(...)

private void NewCar() {
    decimal newCarValue;

    if (decimal.TryParse(vehicleTextBox.Text, out newCarValue))
    {
        // valid decimal.
        // now validate the value of decmial
        if (newCarValue >= 0)
        {
            // All good.  Do the work with car here.
        }
        else
        {
            // complain
        }
    }
    else
    {
        // complain
    }
}

在您的情况下,您正在捕获Exception类,这是一个坏主意。 假设您的代码存在错误,并且在创建汽车时导致NullReferenceException 您将告诉用户他们输入了无效的值,实际上,这只是被掩埋的代码中的错误。

确实,编译器会抱怨,因为除最后一个语句外,所有返回语句均不正确,您没有返回十进制。 我建议您采用此方法之外的MessageBoxes逻辑。 如果您想检查数字是否正确转换,我建议您使用TryParse方法intead:

private void TheFunctionThatWasUsingNewCar() {

    decimal newCar;
    //send the uninitialized newCar variable to TryParse. If everything went ok, the variable will contain the equivalente decimal value.
    if(! Decimal.TryParse(vehicleTextBox.Text, out newCar)){
        //You can be almost certain that if the conversion failed it's because of the format
        MessageBox.Show("Invalid entry (Format Exception). \n" + "Please enter a valid decimal number.");
        KeyBoard.Focus(vehicleTextBox);
        vehicleTextBox.SelectAll();
    }
    //Validate the converted number was not negative
    if(newCar < 0){
        MessageBox.Show("Invalid entry (Negative Value). \n" + "Please enter a valid decimal number.");
        KeyBoard.Focus(vehicleTextBox);
        vehicleTextBox.SelectAll();
    }

    //At this point you have your newCar variable and it's valid.
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM