格式异常在C#中未处理

时间:2016-10-19 05:51:10

标签: c# windows forms decimal

我是初学者,如果可以的话,请解释究竟发生了什么以及为什么?谢谢。我正在创建一个窗口表格来计算员工的总薪酬和佣金,考虑他们的员工类型,他们工作的小时数以及他们赚取的销售收入。例如,我输入S作为员工类型,30小时工作和200销售收入。但我一直得到这个错误,我无法弄清楚为什么。

  

“发生了'System.FormatException'类型的未处理异常   mscorlib.dll中

其他信息:输入字符串的格式不正确。“

此错误即将发生

decimal commission = Convert.ToDecimal(txtSalesCommission.Text);

,特别是它指向(txtSalesCommission.Text)

以下是代码。

private void btnCalculate_Click(object sender, EventArgs e)
{
            string employeeType = txtEmployeeType.Text;
            decimal hours = Convert.ToDecimal(txtHoursWorked.Text);
            decimal revenue = Convert.ToDecimal(txtSalesRevenue.Text);
            decimal commission = Convert.ToDecimal(txtSalesCommission.Text);
            decimal totalPay = Convert.ToDecimal(txtTotalPay.Text);

            txtSalesRevenue.Text = revenue.ToString("c");
            txtSalesCommission.Text = commission.ToString("c");
            txtTotalPay.Text = totalPay.ToString("c");

            //Employee Types: S   = Salaried Employees
            //                HC  = Hourly Commission Employees
            //                HNC = Hourly Non-Commission Employees  

            if (employeeType == "S")

            {
                totalPay = (300 + commission);
                commission = (revenue * .02m);
            }

            else if (employeeType == "HC")

            {
                totalPay = ((12 * hours) + commission);
                commission = (revenue * .01m);
            }

            else if (employeeType == "HNC")
            {
                totalPay = (16 * hours);
                commission = (revenue * 0);
            }

            txtTotalPay.Focus();
}

如果您认为我的代码中的其他情况会再出现这种情况,或者您认为有什么可能是错误的,请告诉我。谢谢!

2 个答案:

答案 0 :(得分:2)

您应该使用TryParse。发生此错误是因为您的字符串无效小数。在这种情况下,如果需要,可以显示错误。

decimal commission = 0;

if(!decimal.TryParse(txtSalesCommission.Text, out commission))
{
     //show error to the user and tell him to fill proper decimal value
     return; //exit the method
}

编辑:msdn中的Decimal.TryParse Method (String, Decimal)文章

答案 1 :(得分:0)

此外,我对tryParse和异常不太熟悉,如果我改为使用Parse并尝试使用catch语句,那会没关系吗?因为我这样做了但是当我输入" S"时,我收到了自己的无效数字格式的失败消息。进入员工类型," 30"进入工作小时数," 200"进入销售收入。

这是我的代码:

private void btnCalculate_Click(object sender, EventArgs e)       
{ 
try            
{
            string employeeType = txtEmployeeType.Text;
            decimal hours = Convert.ToDecimal(txtHoursWorked.Text);
            decimal revenue = Convert.ToDecimal(txtSalesRevenue.Text);
            decimal commission = Decimal.Parse(txtSalesCommission.Text);
            decimal totalPay = Decimal.Parse(txtTotalPay.Text);

            txtSalesRevenue.Text = revenue.ToString("c");
            txtSalesCommission.Text = commission.ToString("c");
            txtTotalPay.Text = totalPay.ToString("c");

            //Employee Types: S   = Salaried Employees
            //                HC  = Hourly Commission Employees
            //                HNC = Hourly Non-Commission Employees  

            if (employeeType == "S")

            {
                totalPay = (300 + commission);
                commission = (revenue * .02m);
            }

            else if (employeeType == "HC")

            {
                totalPay = ((12 * hours) + commission);
                commission = (revenue * .01m);
            }

            else if (employeeType == "HNC")
            {
                totalPay = (16 * hours);
                commission = (revenue * 0);
            }
        }

        catch (FormatException)
        {
            MessageBox.Show(
                "Invalid numeric format. Please check all entries.",
                "Entry Error");
        }
        catch (OverflowException)
        {
            MessageBox.Show(
                "Overflow error. Please enter smaller values.",
                "Entry Error");
        }
        catch (Exception ex)
        {
            MessageBox.Show(
                ex.Message,
                ex.GetType().ToString());

            txtTotalPay.Focus();

        }
    }

    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}