格式异常在c#中是未处理的

本文关键字:未处理 异常 格式 | 更新日期: 2023-09-27 18:13:54

我是初学者,所以如果可以的话,请解释一下到底发生了什么以及为什么?谢谢。我正在创建一个窗体来计算员工的总工资和佣金,同时考虑到员工的类型、工作时间和销售收入。例如,我输入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();
}

如果你认为这将在我的代码的另一种情况下再次出现,或者如果你看到任何可能是错误的,请告诉我。谢谢!

格式异常在c#中是未处理的

您应该使用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

中尝试解析方法(字符串,十进制)文章

我也不太熟悉tryParse和异常,所以它是可以的,如果我做了Parse和做了一个try 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();
    }
}
}