Visual Studio C#异常错误消息

Visual Studio C#异常错误消息,c#,visual-studio-2017,C#,Visual Studio 2017,这是我的密码。当用户单击“计算”按钮时,代码将执行它。但是,如果用户没有输入任何数字,将抛出异常并弹出错误消息。我不希望我的错误消息说“你忘了输入数字!”但自动消息说“输入字符串格式不正确”弹出。如何更改错误消息 private void btnCalculate_Click(object sender, EventArgs e) { try { // Local variables Str

这是我的密码。当用户单击“计算”按钮时,代码将执行它。但是,如果用户没有输入任何数字,将抛出异常并弹出错误消息。我不希望我的错误消息说“你忘了输入数字!”但自动消息说“输入字符串格式不正确”弹出。如何更改错误消息

        private void btnCalculate_Click(object sender, EventArgs e)
    {
        try
        {
            // Local variables

            String num1;
            String num2;
            double number1;
            double number2;
            double result;

            // Get the numbers from the textboxes

            num1 = txtInput1.Text;
            num2 = txtInput2.Text;
            number1 = double.Parse(num1);
            number2 = double.Parse(num2);

            // Determine the user clicks the Addition radiobutton
            if (rdbAdd.Checked)
            {
                // Add two numbers
                result = number1 + number2;
            }
            else
            {
                // Determine the user clicks the Subtraction radiobutton
                if (rdbSubtract.Checked)
                {
                    // Subtract numbers
                    result = number1 - number2;
                }
                else
                {
                    // Determine the user clicks the Multiply radiobutton
                    if (rdbMultiply.Checked)
                    {
                        // Multiply numbers
                        result = number1 * number2;
                    }
                    else
                    {
                        // Devide numbers when the user clicks the Devision radiobutton
                        result = number1 / number2;
                    }
                }
            }

            // Display the result

            txtDisplay.Text = result.ToString();
        }
        catch (Exception ex)
        {
            // Display an error message

            MessageBox.Show(ex.Message);
        }

    }

要显示您选择的邮件

MessageBox.Show("Your message goes here.")
异常有自己的消息,您应该截获您感兴趣的异常类型,并显示适合该异常的消息。如果文本字段中没有任何内容,则
Double.Parse
抛出异常(查看它抛出的异常)

但是,如果number2为零,并且用户选择“除法”,您将得到一个不同的异常(被零除法)


通常,您应该验证您的输入,并且只需使用
Double即可。您可能只需要解析
。但通常,你需要更多。此外,如果您打算将应用程序国际化,则需要根据区域设置进行解析。请参阅上面的链接,这里有一种本地化解析的方法。

要显示您选择的消息

MessageBox.Show("Your message goes here.")
异常有自己的消息,您应该截获您感兴趣的异常类型,并显示适合该异常的消息。如果文本字段中没有任何内容,则
Double.Parse
抛出异常(查看它抛出的异常)

但是,如果number2为零,并且用户选择“除法”,您将得到一个不同的异常(被零除法)


通常,您应该验证您的输入,并且只需使用
Double即可。您可能只需要解析
。但通常,你需要更多。此外,如果您打算将应用程序国际化,则需要根据区域设置进行解析。请参阅上面的链接,这里有一个本地化解析的方法。

这是此异常的默认消息,它是
格式异常

您可以捕获此类异常,然后只显示自己的消息:

   try
   {
    .... your code ...
   }
   catch (FormatException ex)
   {
        //FormatException was thrown, display your message
        MessageBox.Show("You forgot to put the number!");
   }
   catch (Exception ex)
   {
        // Some other kind of exception was thrown ...
        MessageBox.Show(ex.Message);
   }

这是此异常的默认消息,它是
FormatException

您可以捕获此类异常,然后只显示自己的消息:

   try
   {
    .... your code ...
   }
   catch (FormatException ex)
   {
        //FormatException was thrown, display your message
        MessageBox.Show("You forgot to put the number!");
   }
   catch (Exception ex)
   {
        // Some other kind of exception was thrown ...
        MessageBox.Show(ex.Message);
   }
您可以有几个“catch”子句,每种类型的异常对应一个:

try
{
    // Your code goes here
}
catch (DivideByZeroException ex)
{
    MessageBox.Show("Cannot divide by zero! " + ex.Message);
}
catch (Exception ex)
{
    // This is a generic exception
    MessageBox.Show("Error: " + ex.Message);
}
您必须将它们从更具体的到更一般的顺序排列。

您可以有几个“catch”子句,对于要处理的每种类型的异常都有一个:

try
{
    // Your code goes here
}
catch (DivideByZeroException ex)
{
    MessageBox.Show("Cannot divide by zero! " + ex.Message);
}
catch (Exception ex)
{
    // This is a generic exception
    MessageBox.Show("Error: " + ex.Message);
}

您必须将它们从更具体的到更一般的顺序排列。

如果您尝试使用此选项,那么如果txtInput1和txtInput2中的一个为null或为空,则无法继续

if(string.IsNullOrWhiteSpace(txtInput1.Text) == true)
{
    MessageBox.Show("Your message goes here.");
    return; // this is important, return if true
}

if(string.IsNullOrWhiteSpace(txtInput2.Text) == true)
{
    MessageBox.Show("Your message goes here.");
    return; // this is important, return if true
}

          // then
. . . . . // proceed if no problem found

如果您尝试此方法,那么如果txtInput1和txtInput2中的一个为null或空,则无法继续

if(string.IsNullOrWhiteSpace(txtInput1.Text) == true)
{
    MessageBox.Show("Your message goes here.");
    return; // this is important, return if true
}

if(string.IsNullOrWhiteSpace(txtInput2.Text) == true)
{
    MessageBox.Show("Your message goes here.");
    return; // this is important, return if true
}

          // then
. . . . . // proceed if no problem found

没有必要用“你做错了”的消息打用户的脸。实际上是你做错了。只是在用户输入内容之前不要启用按钮。使用TextChanged事件。使用ErrorProvider来帮助他纠正错误。没有必要用“你做错了”的消息来打用户的脸。实际上是你做错了。只是在用户输入内容之前不要启用按钮。使用TextChanged事件。使用ErrorProvider来帮助他正确操作。