【问题标题】:Raising a error when trying to save a empty listbox尝试保存空列表框时引发错误
【发布时间】:2010-10-12 09:19:08
【问题描述】:

当用户单击保存并且列表框中没有任何内容时,我想引发错误。 我想我会像这样使用 try catch 块:

try
{
   //when you go to save, and the list box is empty, you should get an error message
   if (dataListBox.Items.Equals(null))
      throw new Exception();

   //i wanted to save on the form_close event, so i put the save data into a method and just call the method from this event 
   this.save();
}
catch (Exception err)
{
   //spits out the errors if there are any
   MessageBox.Show(err.Message, "List Box is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

但这对我不起作用。它仍然保存并且没有消息框出现。

【问题讨论】:

    标签: c# winforms listbox


    【解决方案1】:

    根本不要这样做。比较:

    try
    {
    //when you go to save, and the list box is empty, you should get an error message
        if (dataListBox.Items.Count != 0)
            throw new Exception("Please add at least one item to the list.");
    
        this.save();
    }
    catch (Exception err)
    {
        //spits out the errors if there are any
        MessageBox.Show(err.Message, "List Box is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    

    与:

        if (dataListBox.Items.Count != 0)
        {
            MessageBox.Show("Please add at least one item to the list.", "List Box is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            this.save();
        }
    
    1. 不对控制流使用异常会更有效。是的,效率不是一切,但过早悲观也没有意义。
    2. 验证失败不是例外,因为我们期望它们。如果代码已经到了尝试保存的地步,并且由于缺少数据而在逻辑上是不可能的,那么 可能 是一个异常(但应该在更高版本的验证中被捕获) up 代码 - 特别是 this 代码)。如果由于文件系统错误、数据库连接错误等原因导致保存失败,那么这是一个例外。用户验证虽然不是例外,只有在我们需要与更高级别的代码对话时才应该报告(同样,如果可能的话,该代码应该通过控制流机制捕获它)。李>
    3. 它可以更容易地发现逻辑错误。因为我们已经区分了异常和非异常情况,我们可以看到我们并不是在寻找真正可能的异常。照原样,如果您让用户正确填写列表,但由于真正的异常导致保存失败,您将在消息框的标题中说“列表框为空”,这会使用户感到困惑。现在这变得更清楚了,并且更容易修复该错误:

      if (dataListBox.Items.Count != 0)
      {
          MessageBox.Show("Please add at least one item to the list.", "List Box is empty", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }
      else
      {
          try
          {
              this.save();
          }
          catch(Exception ex)
          {
               MessageBox.Show("Saving failed.\n Technical details:\n" + ex.Message, "Saving Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
          }
      }
      

    【讨论】:

    • 感谢您的遮阳篷,看着它,它看起来不错,但是当我将它添加到我的代码中时,它不起作用。
    • 当我将一个项目添加到列表框时,当我去保存时它仍然说那里什么都没有..
    【解决方案2】:

    尝试添加这个:

    if(dataListBox.Items.Count==0)
                throw new Exception();
    

    顺便说一句,试着在这里定义你自己的异常类,例如 EmptyListException,那么你就可以确定你捕捉到了你想要捕捉的东西。现在,此代码也将针对“Save”方法中引发的异常显示相同的 MessageBox。

    【讨论】:

    • 你确定吗?据我了解:列表框是空的,没有引发异常。我对吗?尝试在调试模式下检查 dataListBox.Items.Count。它等于零吗?
    【解决方案3】:

    您应该改用 .Count()。

    if (dataListBox.Items.Count < 1)
        throw new Exception();
    

    【讨论】:

      【解决方案4】:

      您需要检查:

      if(dataListBox.Items.Count ==0)
         throw new Exception();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多