Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
异常处理C#_C#_Visual Studio 2012_Exception Handling_Windows Forms Designer - Fatal编程技术网

异常处理C#

异常处理C#,c#,visual-studio-2012,exception-handling,windows-forms-designer,C#,Visual Studio 2012,Exception Handling,Windows Forms Designer,我在这方面相对较新,一直在绞尽脑汁试图让我的程序正常工作,但它就是不能。我在Visual Studio 2012 C#中处理表单应用程序 当用户输入值大于0但小于10000时,我需要它生成一条明显的错误消息。当用户输入非数字值时,它还必须生成一条明确的错误消息;当用户根本无法输入任何值时,它还必须生成一条明确的错误消息 到目前为止,我编写的代码在用户输入非数字值或根本无法输入任何值时会生成一条明显的错误消息,但当用户输入的值低于或超过所需范围时,它不会触发错误消息 这就好像编译器忽略了我为第一个

我在这方面相对较新,一直在绞尽脑汁试图让我的程序正常工作,但它就是不能。我在Visual Studio 2012 C#中处理表单应用程序

当用户输入值大于
0
但小于
10000
时,我需要它生成一条明显的错误消息。当用户输入非数字值时,它还必须生成一条明确的错误消息;当用户根本无法输入任何值时,它还必须生成一条明确的错误消息

到目前为止,我编写的代码在用户输入非数字值或根本无法输入任何值时会生成一条明显的错误消息,但当用户输入的值低于或超过所需范围时,它不会触发错误消息

这就好像编译器忽略了我为第一个异常/溢出异常编写的代码,而只识别了第二个也是最后一个异常的代码。我的代码没有编码错误。看来我的问题出在逻辑上

如果可以的话,请帮助我。我的代码在下面,谢谢

    private void btnCalculate_Click(object sender, System.EventArgs e)
    {

        try
        {
            {

                decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
                decimal discountPercent = .25m;
                decimal discountAmount = subtotal * discountPercent;
                decimal invoiceTotal = subtotal - discountAmount;

                lblDiscountPercent.Text = discountPercent.ToString("p1");
                lblDiscountAmount.Text = discountAmount.ToString("c");
                lblTotal.Text = invoiceTotal.ToString("c");

            }
        }
        catch (OverflowException)

        {
            decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);

            if (subtotal <= 0)
            {
                MessageBox.Show("Subtotal must be greater than $0.00. ", "Error Entry");
                txtSubtotal.Focus();
            }

             if (subtotal >= 10000)

            {
                MessageBox.Show("Subtotal must be less than $10000.00. ", "Error Entry");
                txtSubtotal.Focus();
            }
        }

        catch (Exception)
        {

            if (txtSubtotal.Text == "")
            {
                MessageBox.Show("Subtotal is a required field. ", "Error Entry");
            }


            else
            {
                MessageBox.Show(
                 "Please enter a valid Number for the subtotal field.", "Error Entry");
                txtSubtotal.Focus();
            }


            }           

        }




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

    private void txtSubtotal_TextChanged(object sender, EventArgs e)
    {

    }

}
private void btnCalculate\u单击(对象发送者,System.EventArgs e)
{
尝试
{
{
十进制小计=Convert.ToDecimal(txtSubtotal.Text);
小数折扣百分比=0.25百万;
小数折扣额=小计*折扣百分比;
十进制发票总额=小计-折扣金额;
LBLDiscontPercent.Text=折扣百分比.ToString(“p1”);
lblDiscountAmount.Text=discountAmount.ToString(“c”);
lblTotal.Text=invoiceTotal.ToString(“c”);
}
}
捕获(溢出例外)
{
十进制小计=Convert.ToDecimal(txtSubtotal.Text);
如果(小计=10000)
{
MessageBox.Show(“小计必须小于$10000.00。”,“错误输入”);
txtSubtotal.Focus();
}
}
捕获(例外)
{
如果(txtSubtotal.Text==“”)
{
Show(“小计是必填字段。”,“错误输入”);
}
其他的
{
MessageBox.Show(
“请为小计字段输入有效数字。”,“错误输入”);
txtSubtotal.Focus();
}
}           
}
私有void btnExit\u单击(对象发送者,System.EventArgs e)
{
这个。关闭();
}
私有void txtSubtotal_TextChanged(对象发送方,事件参数e)
{
}
}

}/P>< P>正如我在评论中提到的,您应该考虑将代码移到catch块之外。

在这种情况下,您应该考虑创建一个简单的方法来验证您的输入并生成输出消息

举个例子:

  private bool IsPageValid()  
  {
           string errorMessage = string.Empty;               
           bool isValid = true;
           if (subtotal <= 0)
           {
           errorMessage+="<li>"+"<b>Subtotal must be greater than $0.00. ", "Error Entry"+ "</b><br/>";
           txtSubtotal.Focus();
           isValid=false;
           }
   }

代码应该是这样的

try {
    decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
    try {
        if(x<0 || x>10000){
            throw new OverFlowException("");
        }
        //Do what ever you want
    }
    catch(Exception ex){
        // catch overflow
    }
}
catch(Exception ex){
    // catch nonnumeric value
}
试试看{
十进制小计=Convert.ToDecimal(txtSubtotal.Text);
试一试{
若有(x10000){
抛出新的OverFlowException(“”);
}
//你想干什么就干什么
}
捕获(例外情况除外){
//捕获溢出
}
}
捕获(例外情况除外){
//捕获非数值
}

我将使用KeyEvent按enter或Leave事件来进行此操作,首先,如果用户的输入不是字符串,我需要创建通用类进行验证。 条件: 1使用通用类验证输入是否不是字符串Im

   public class iCnF()
   {
    public static System.Boolean IsNumeric(System.Object Expression) 
      {
        if (Expression == null || Expression is DateTime)
            return false;
        if (Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
            return true;
        try 
        {
            if(Expression is string)
                Double.Parse(Expression as string);
            else
                Double.Parse(Expression.ToString());
                return true;
        } catch {} // just dismiss errors but return false
            return false;
     }
}
  • 然后我需要验证输入是否为空

       private void txtSubtotal_KeyDown(object sender, KeyEventArgs e)
       {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
        {
            if (txtSubtotal.Text.Length > 0)
            {
                bool sd = iCnF.IsNumeric(txtSubtotal.Text);
                if (sd == false)
                {
                   MessageBox.Show("Subtotal must be a numeric value. ", "Error Entry");
    
                   txtSubtotal.Clear();
                   txtSubtotal.Focus();
                }
                else
                {
                    decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
                    if (subtotal <= 0)
                    {
                        MessageBox.Show("Subtotal must be greater than $0.00. ", "Error Entry");
                        txtSubtotal.Focus();
                   }
    
                   if (subtotal >= 10000)
                   {
                       MessageBox.Show("Subtotal must be less than $10000.00. ", "Error Entry");
                       txtSubtotal.Focus();
                   }
               }
           }
           else
           {
               MessageBox.Show("Subtotal must not be empty. ", "Error Entry");
               txtSubtotal.Focus();
           }
       }
      }  
    
    private void txtssubtotal\u KeyDown(对象发送方,KeyEventArgs e)
    {
    如果(e.KeyCode==键。输入| | e.KeyCode==键。选项卡)
    {
    如果(txtSubtotal.Text.Length>0)
    {
    bool sd=iCnF.IsNumeric(txtSubtotal.Text);
    如果(sd==false)
    {
    Show(“小计必须是数值。”,“错误输入”);
    txtSubtotal.Clear();
    txtSubtotal.Focus();
    }
    其他的
    {
    十进制小计=Convert.ToDecimal(txtSubtotal.Text);
    如果(小计=10000)
    {
    MessageBox.Show(“小计必须小于$10000.00。”,“错误输入”);
    txtSubtotal.Focus();
    }
    }
    }
    其他的
    {
    Show(“小计不能为空。”,“错误输入”);
    txtSubtotal.Focus();
    }
    }
    }  
    
  • 如果不是空的,则数字值my小计=10000


  • 希望这能帮助您:D

    将您的代码移出异常块,当他们单击按钮或其他内容时,您首先要做的就是发布所有代码(特别是try块中的代码)。不会引发溢出异常(至少在默认情况下)如果输入超出0和10000。为什么它应该是异常的一部分?您只需编写一个验证类,在执行任何操作之前,您应该在其中返回true或false。如果您的代码语法正确并且在运行时没有中断,则您的程序将永远不会捕获块。设置断点(F9)检查代码的哪一部分实际执行。显然,从发布的行中没有任何内容。我正在尝试发布try块的其余部分,但注释部分有字符限制。此外,如果他希望在无效值上有一个红色边框,他可以在文本框的TextChanged回调中调用IsPageValid(),我更喜欢在应用程序中使用。非常感谢。我正在根据Murach培训手册完成这些任务,并试图对现实世界中的编程有一个良好的感觉。你给了我一些很好的建议,对此我表示感谢。用户3473475没问题,请标记它检查我的答案以供参考,对于有相同问题的人。谢谢
       private void txtSubtotal_KeyDown(object sender, KeyEventArgs e)
       {
        if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
        {
            if (txtSubtotal.Text.Length > 0)
            {
                bool sd = iCnF.IsNumeric(txtSubtotal.Text);
                if (sd == false)
                {
                   MessageBox.Show("Subtotal must be a numeric value. ", "Error Entry");
    
                   txtSubtotal.Clear();
                   txtSubtotal.Focus();
                }
                else
                {
                    decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
                    if (subtotal <= 0)
                    {
                        MessageBox.Show("Subtotal must be greater than $0.00. ", "Error Entry");
                        txtSubtotal.Focus();
                   }
    
                   if (subtotal >= 10000)
                   {
                       MessageBox.Show("Subtotal must be less than $10000.00. ", "Error Entry");
                       txtSubtotal.Focus();
                   }
               }
           }
           else
           {
               MessageBox.Show("Subtotal must not be empty. ", "Error Entry");
               txtSubtotal.Focus();
           }
       }
      }