简体   繁体   中英

Visual Studio C# Exception error message

Here is my code. when the user clicks the "calculate" button, the code will execute it. However, if the user doesn't put any number, the exception throws and the error message will pop up. I wan't my error message says that "You forgot to put the number!" but the automatic message that says "Input string was not in a correct format" pops up. How to change the error message?

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

    }

To display your choice of messages...

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

The exception has it's own message, you should intercept the type of exception you are interested in and display your message approriate to the exception. If there is nothing in the text fields, then Double.Parse throws the exception (look at Double.Parse for the Exceptions it throws)

But if number2 is zero, and the user chooses to "divide", you will get a different exception (divide by zero).

Generally, you should validate your input, and simply using Double.Parse might be all you need. But typically, you need more. Also, if you intend to internationalize your application, you need to parse according to locale. See the link above, there is a method for localized parsing.

This is the default message of this exception which is a FormatException .

You can catch that kind of exceptions and then just display your own message:

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

You can have several "catch" clauses, one for each type of exception that you want to handle:

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

You must order them from more specific to more generic.

may be you try this one, you cannot proceed if one of the txtInput1 and txtInput2 are null or Empty.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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