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.";
}
}
C# Try Catch with Switch statement
Page 1 of 14 Replies - 1153 Views - Last Post: 03 October 2012 - 07:33 AM
#1
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.
Replies To: C# Try Catch with Switch statement
#2
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.
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.
#3
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.
Or use Double.TryParse Method.
This post has been edited by zeeshanef: 30 September 2012 - 02:59 AM
#4
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.
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.
#5
Re: C# Try Catch with Switch statement
Posted 03 October 2012 - 07:33 AM
jrr34td, 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.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote






|