C# 在文本框中输入并按下按钮时处理错误的异常

C# 在文本框中输入并按下按钮时处理错误的异常,c#,winforms,wcf,exception-handling,C#,Winforms,Wcf,Exception Handling,当我正确输入了账号和取款金额时,我会不断收到“输入整数”消息框 假设我为账号输入了“12”,为金额输入了“50”(或“50.0”)——我会收到“输入整数”异常消息框 如果我什么也不输入,我将得到“输入账号”,这是正确的 如果我只输入账号(不管账号是否存在),但将金额保留为空,则按“取款”按钮不会得到任何结果 如果我输入账号和金额(错误或正确,无所谓),我会收到“输入和整数”异常消息框 我哪里出错了 private void btnWithdraw_Click(object sender, Eve

当我正确输入了账号和取款金额时,我会不断收到“输入整数”消息框

假设我为账号输入了“12”,为金额输入了“50”(或“50.0”)——我会收到“输入整数”异常消息框

如果我什么也不输入,我将得到“输入账号”,这是正确的

如果我只输入账号(不管账号是否存在),但将金额保留为空,则按“取款”按钮不会得到任何结果

如果我输入账号和金额(错误或正确,无所谓),我会收到“输入和整数”异常消息框

我哪里出错了

private void btnWithdraw_Click(object sender, EventArgs e)
        {
            if (!txtSearch.Text.Equals(""))
            {
                if(!txtAmount.Text.Equals(""))
                {
                    try
                    {
                        int aN = int.Parse(txtSearch.Text);
                        double am = double.Parse(txtAmount.Text);
                        client.Withdraw(aN, am);
                        MessageBox.Show(String.Format("Withdrawn {0} from {1}\nBalance now: {2}", am, aN));
                        //if(client.Fi)
                        //    MessageBox.Show(String.Format("Customer {0} couldn't be found", aN));
                        //else
                        //    MessageBox.Show(String.Format("Customer {0}\nBalance: {1}C", aN, client.CustomerBalance(aN).ToString()));

                    }
                    catch (FormatException)
                    {
                        MessageBox.Show("Enter an integer");
                    }
                    catch (NullReferenceException)
                    {
                        MessageBox.Show("Customer cannot be found");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
            }
            else
            {
                MessageBox.Show("Enter account number");
            }
        }

你为什么不能这样做呢:

int aN;
double am;
if(int.TryParse(txtSearch.Text,out aN) && double.TryParse(txtAmount.Text,out am))
{
    client.Withdraw(aN, am);
    MessageBox.Show(String.Format("Withdrawn {0} from {1}\nBalance now: {2}", am, aN));
}
else
{
    //Do something
}

你为什么不能这样做呢:

int aN;
double am;
if(int.TryParse(txtSearch.Text,out aN) && double.TryParse(txtAmount.Text,out am))
{
    client.Withdraw(aN, am);
    MessageBox.Show(String.Format("Withdrawn {0} from {1}\nBalance now: {2}", am, aN));
}
else
{
    //Do something
}

格式字符串指定了三个参数,但您只提供了两个:

String.Format("Withdrawn {0} from {1}\nBalance now: {2}", am, aN)
这会抛出一个
FormatException
,但不是编写
FormatException
catch块时所想到的

因此,为了避免这种异常,您需要一种方法来获取变量中的新平衡,该变量可以传递到
String.Format
。(您最好使用比
aN
am
更长、更具描述性的变量名)

对于异常处理的问题,最直接的答案是为方法执行的单独操作使用单独的try块,即解析两个不同的字符串、执行事务、为用户设置消息格式并显示该消息。这将允许将
int.Parse
引发的
FormatException
的处理与
string.Format
引发的
FormatException
的处理分开


但是,正如Arion所建议的,对于解析用户输入,通常最好使用
TryParse
而不是捕获异常(捕获
FormatException
的问题就是一个很好的例子!)。当然,这假设您使用的框架版本具有
TryParse
方法;它们是在2.0版中添加的。

格式字符串指定了三个参数,但您只提供了两个:

String.Format("Withdrawn {0} from {1}\nBalance now: {2}", am, aN)
这会抛出一个
FormatException
,但不是编写
FormatException
catch块时所想到的

因此,为了避免这种异常,您需要一种方法来获取变量中的新平衡,该变量可以传递到
String.Format
。(您最好使用比
aN
am
更长、更具描述性的变量名)

对于异常处理的问题,最直接的答案是为方法执行的单独操作使用单独的try块,即解析两个不同的字符串、执行事务、为用户设置消息格式并显示该消息。这将允许将
int.Parse
引发的
FormatException
的处理与
string.Format
引发的
FormatException
的处理分开


但是,正如Arion所建议的,对于解析用户输入,通常最好使用
TryParse
而不是捕获异常(捕获
FormatException
的问题就是一个很好的例子!)。当然,这假设您使用的框架版本具有
TryParse
方法;它们是在2.0版中添加的。

谢谢。问题解决了。以后我会认真注意变量的命名。谢谢。问题解决了。以后我会认真地注意变量的命名。