4 Replies - 1418 Views - Last Post: 24 April 2010 - 05:49 PM Rate Topic: -----

#1 Guest_shadowhand18*


Reputation:

Try/Catch block help

Posted 24 April 2010 - 02:05 PM

sup guys I need help a piece of coding for a homework assignment. Ok I am doing coding with the IF and Switch statements. I have everything running perfectly, but i need to incorporate the Try/Catch block statments. My question is how and where do i put the Try/catch block? I need it to catch an error where the user inputs a month that is not available. to be more in-depth, the user need to enter a number from 1-12 (represents the months) if the user enters lets say 17, the try/catch statement need to catch it and tell the user its an invalid number. Here is my code below:


 private void shownumberButton_Click(object sender, EventArgs e)
        {

                if (textBox.Text == "1")          //Ok everything is explains the IF coding and how its used
                {
                    usingifLabel.Text = "31";

                }
                else if (textBox.Text == "2")
                {
                    usingifLabel.Text = "28";
                }
                else if (textBox.Text == "3")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "4")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "5")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "6")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "7")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "8")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "9")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "10")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "11")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "12")
                {
                    usingifLabel.Text = "31";
                }

                //Ok here is the Switch statements and how its used
                string MonthNumEntered = textBox.Text;

                switch (MonthNumEntered)
                {
                    case "1":
                        usingswitchLabel.Text = "31";
                        break;
                    case "2":
                        usingswitchLabel.Text = "28";
                        break;
                    case "3":
                        usingswitchLabel.Text = "31";
                        break;
                    case "4":
                        usingswitchLabel.Text = "30";
                        break;
                    case "5":
                        usingswitchLabel.Text = "31";
                        break;
                    case "6":
                        usingswitchLabel.Text = "30";
                        break;
                    case "7":
                        usingswitchLabel.Text = "31";
                        break;
                    case "8":
                        usingswitchLabel.Text = "31";
                        break;
                    case "9":
                        usingswitchLabel.Text = "30";
                        break;
                    case "10":
                        usingswitchLabel.Text = "31";
                        break;
                    case "11":
                        usingswitchLabel.Text = "30";
                        break;
                    case "12":
                        usingswitchLabel.Text = "31";
                        break;





                }
            }



Is This A Good Question/Topic? 0

Replies To: Try/Catch block help

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Try/Catch block help

Posted 24 April 2010 - 02:26 PM

Actually, a try..catch block is not what you need here.

You are in an event handler for shownumberButton_Click(). The textBox.Text value will not change while you are inside this handler so 'catching' a bad value, telling the user to reenter it, and looping so you can get the new user input won't work.

Instead, add a last else:

...
else if (textBox.Text == "12")
{
   usingifLabel.Text = "31";
}
else  // not a valid input
{
// do something like:
   MessageBox.Show("Please enter a legal month number: 1-12");
   textbox.Clear(); // remove bad text
   return;  // exit event handler
}
...



If you were using just the switch statement you could add a default: block and do the same sort of error handling.

This post has been edited by n8wxs: 24 April 2010 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_shadowhand18*


Reputation:

Re: Try/Catch block help

Posted 24 April 2010 - 02:40 PM

I actually thought about the return value, but for the grading rubric I have to use the try/catch block. I dont even know where to begin to
put the code. That was my problem. the rubric says :

Use try/catch block to insure numeric data is entered.
Was This Post Helpful? 0

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Try/Catch block help

Posted 24 April 2010 - 03:45 PM

ok, then wrap the try() block around your if() and switch blocks.

As the first statement inside the try() use something like

private void shownumberButton_Click(object sender, EventArgs e)
{
   try
   {
      int num = Int32.Parse(textbox.Text);

      if (num < 1 || num > 12)
          throw new Exception("Invalid month number");

   } catch(Exception ex) {
          MessageBox.Show(ex.Message);
          textbox.Clear();
          return; // exit handler
   }
   // original code below here
   ...


This post has been edited by n8wxs: 24 April 2010 - 03:59 PM

Was This Post Helpful? 1
  • +
  • -

#5 Guest_shadowhand18*


Reputation:

Re: Try/Catch block help

Posted 24 April 2010 - 05:49 PM

Ah got it!!! ok here is the final code thank you so much for all your help!!

  private void shownumberButton_Click(object sender, EventArgs e)
        {
            try
            {
                int num = Int32.Parse(textBox.Text);

                if (num < 1 || num > 12)
                    throw new Exception("Invalid month number");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                textBox.Clear();
                return; // exit handler
            }

          
                if (textBox.Text == "1")          //Ok everything here explains the IF coding and how its used
                {
                    usingifLabel.Text = "31";

                }
                else if (textBox.Text == "2")
                {
                    usingifLabel.Text = "28";
                }
                else if (textBox.Text == "3")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "4")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "5")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "6")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "7")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "8")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "9")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "10")
                {
                    usingifLabel.Text = "31";
                }
                else if (textBox.Text == "11")
                {
                    usingifLabel.Text = "30";
                }
                else if (textBox.Text == "12")
                {
                    usingifLabel.Text = "31";
                }
            
            

                //Ok here is the Switch statements and how its used
                   string MonthNumEntered = textBox.Text;
        
                switch (MonthNumEntered)
                {
                    case "1":
                        usingswitchLabel.Text = "31";
                        break;
                    case "2":
                        usingswitchLabel.Text = "28";
                        break;
                    case "3":
                        usingswitchLabel.Text = "31";
                        break;
                    case "4":
                        usingswitchLabel.Text = "30";
                        break;
                    case "5":
                        usingswitchLabel.Text = "31";
                        break;
                    case "6":
                        usingswitchLabel.Text = "30";
                        break;
                    case "7":
                        usingswitchLabel.Text = "31";
                        break;
                    case "8":
                        usingswitchLabel.Text = "31";
                        break;
                    case "9":
                        usingswitchLabel.Text = "30";
                        break;
                    case "10":
                        usingswitchLabel.Text = "31";
                        break;
                    case "11":
                        usingswitchLabel.Text = "30";
                        break;
                    case "12":
                        usingswitchLabel.Text = "31";
                        break;
                        {
                        }

                }
            }


Was This Post Helpful? 0

Page 1 of 1