2017-09-17 77 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