Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3223792
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:08:53+00:00 2026-05-17T16:08:53+00:00

When the user clicks save and there is nothing in the listbox, I want

  • 0

When the user clicks save and there is nothing in the listbox, I want to raise an error.
I figured I would use a try catch block like so:

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);
}

But this isn’t working for me. It still saves and no message box comes up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:08:53+00:00Added an answer on May 17, 2026 at 4:08 pm

    Don’t do so at all. Compare:

    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);
    }
    

    With:

        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. It’s more efficient to not use exceptions for control flow. Yeah, efficiency isn’t everything, but there’s no point in premature pessimisation either.
    2. Validation failures are not exceptions, because we expect them. If the code had got to the point where it was trying to save and that was logically impossible due to the lack of data then that might be an exception (but one that should have been caught in validation in higher up code – specifically this code). If saving fails because of a filesystem error, db connection error etc., then that’s an exception. User validation though is something that is not an exception and should only be reported as such if we need to talk to a higher level of code (again, that code should have caught it if at all possible through control-flow mechanisms).
    3. It makes it easier to spot the logical bug. Because we’ve separated exceptional and non-exceptional cases, we can see that we aren’t looking for the real possible exception. As is, if you had a user fill in the list properly, but the save failed because of a real exception, you are going to say “List box is empty” in your message box’s caption, which will confuse the user. This becomes plainer now, and it’s easier to fix that bug:

      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);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When user clicks on select box , I want to hide the options menu
When a user clicks a submit button I want the form to be submitted.
Use case: user clicks the link on a webpage - boom! load of files
When the user clicks a button, I want his browser to automatically scroll to
When a user clicks on a button or link, I use the SimpleModal jQuery
When the user clicks an Edit button on my form, I want a box
I want to have the JQuery Datepicker open when the user clicks on an
I have a btnSave_Click() function in my code-behind. If a user clicks the save
When the user clicks on a row in the datagrid (or uses the keyboard),
When the user clicks on a link to generate report I make an AJAX

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.