2017-09-17 73 views
1

这是我的代码。当用户点击“计算”按钮时,代码将执行它。但是,如果用户没有输入任何数字,则会引发异常并显示错误消息。我不是我的错误消息说:“你忘了把数字!”但是弹出“输入字符串格式不正确”的自动消息。如何更改错误信息?Visual Studio C#异常错误消息

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

    } 
+2

没有必要用“你做错了假人”的信息来抨击用户。实际上你是错的。只需在用户输入内容之前不启用该按钮。使用TextChanged事件。使用ErrorProvider来帮助他解决问题。 –

回答

1

要显示邮件的选择...

MessageBox.Show("Your message goes here.") 

异常都有它自己的消息,你应该拦截例外您有兴趣的类型,并显示您的消息approriate到异常。如果没有在文本字段,然后Double.Parse抛出异常(看Double.Parse它抛出的异常)

但如果NUMBER2为零,用户选择“分”,你会得到一个不同的异常(除以零)。

通常,您应该验证您的输入,并且只需使用Double.Parse即可。但通常情况下,您需要更多。另外,如果您打算将您的应用程序国际化,则需要根据区域设置进行解析。看到上面的链接,有一个本地化解析的方法。

1

这是此异常的默认消息,它是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); 
    } 
1

你可以有你要处理几个“抓”条款,每一个类型的异常:

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

您必须从更具体到更通用的顺序对它们进行排序。

1

可能是您尝试这一个,如果其中一个txtInput1和txtInput2为空或空,则无法继续。

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