4 Replies - 3553 Views - Last Post: 03 October 2012 - 07:33 AM Rate Topic: -----

#1 jrr34td  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 29-September 12

C# Try Catch with Switch statement

Posted 29 September 2012 - 05:45 PM

I have an assignment where I am to receive numeric input for 3 grades and I have to convert it to a letter grade using a switch statement. I also have to use a Try/Catch. The code works fine without the Try/Catch, but when the Try/Catch is used, the catch is taking over, giving the false Label Return value. This is the cs code for the button the user clicks after inserting the data.

   protected void ButtonCompute_Click(object sender, EventArgs e)
    {
        try
        {

            Double Grade1, Grade2, Grade3, GradeAverage;
            Grade1 = Double.Parse(TextBoxGrade1.Text);
            Grade2 = Double.Parse(TextBoxGrade2.Text);
            Grade3 = Double.Parse(TextBoxGrade3.Text);
            GradeAverage = ((Grade1 + Grade2 + Grade3) / 3);
            LabelAverage.Text = GradeAverage.ToString("#.##");

            int GradeSwitch = Int32.Parse(LabelAverage.Text);

            switch (GradeSwitch / 10)
            {

                case 9:
                    {
                        LabelLetterGrade.Text = "A";
                        ImageGrade.ImageUrl = "Images/A.jpg";
                        break;
                    }
                case 8:
                    {
                        LabelLetterGrade.Text = "B";
                        ImageGrade.ImageUrl = "Images/B.jpg";
                        break;
                    }
                case 7:
                    {
                        LabelLetterGrade.Text = "C";
                        ImageGrade.ImageUrl = "Images/C.jpg";
                        break;
                    }
                case 6:
                    {
                        LabelLetterGrade.Text = "D";
                        ImageGrade.ImageUrl = "Images/D.jpg";
                        break;
                    }
                default:
                    {
                        LabelLetterGrade.Text = "F";
                        ImageGrade.ImageUrl = "Images/F.jpg";
                        break;
                    }

            }
            ImageGrade.Visible = true;
            LabelMessage.Text = "";
        }
        catch
        {
            LabelMessage.Text = "Error, please enter valid scores.";
        }

    }



Is This A Good Question/Topic? 0
  • +

Replies To: C# Try Catch with Switch statement

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,217
  • Joined: 02-June 10

Re: C# Try Catch with Switch statement

Posted 29 September 2012 - 09:32 PM

Catch can only happen if your code is throwing an exception. Period. That's just how it works.
So find the error that is throwing the exception. There is a link in my signature for "What does this error mean?" and that would be a good article to start with.
Was This Post Helpful? 1
  • +
  • -

#3 zeeshanef  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 76
  • Joined: 14-April 12

Re: C# Try Catch with Switch statement

Posted 30 September 2012 - 02:58 AM

Exceptions can occur, where you are parsing the values like Double.Parse(), so put those statements in try.
Or use Double.TryParse Method.

This post has been edited by zeeshanef: 30 September 2012 - 02:59 AM

Was This Post Helpful? 0
  • +
  • -

#4 November-06  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 408
  • Joined: 04-January 11

Re: C# Try Catch with Switch statement

Posted 30 September 2012 - 07:58 PM

Validate inputs checking if they are numeric or not to prevent errors. It is not a good practice to catch exceptions to validate numeric values.

Debug the code to see which line caused the exception to be thrown. Tell us what the ex.Message is so that we would know what the exact error is.
Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: C# Try Catch with Switch statement

Posted 03 October 2012 - 07:33 AM

View Postjrr34td, on 29 September 2012 - 07:45 PM, said:

The code works fine without the Try/Catch, but when the Try/Catch is used, the catch is taking over, giving the false Label Return value.


This is an incorrect statement. The code isn't working without the try/catch, if it fails with the try/catch. Just because it does something you want it to do, doesn't mean that it isn't doing (or trying to do) something that you don't want. Each previous post contains something that you should do: check out tlhIn`toq's tutorial on error messages, and the rest of them, for that matter: they all contain good information, and I've learned a lot from them. Follow the next two posts' suggestions, too, because killing a try/catch to make your program work is just bad. The try/catch is trying to tell you something, and you shouldn't ignore it. TryParse is your friend (good friends tell you when you're wrong, rather than just let you screw up, like Parse does), and the error message that November-06 is asking for will tell you what's wrong.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1