TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Henry Lhteenmaki
NA
13
12.3k
Error handler
Dec 11 2011 4:17 AM
Hi !
I tried to make some kind of control on the user input so the Text Box can only allow for numeric values to be accepted , otherwise the program will throw an error exception , but I don't know why the exception handler did not work as I expect.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
int[] testScores = new int[3];
void initGrades() {
try
{
testScores[0] = int.Parse(txtScore1.Text);
}
catch (FormatException)
{
Console.WriteLine("The value {0}, Is not valid", txtScore1.Text);
}
catch (Exception exception) {
Console.WriteLine("Unexpected Error : {0}",exception.Message);
}
finally {
Console.WriteLine("Thanks");
}
try
{
testScores[1] = int.Parse(txtScore2.Text);
}
catch (FormatException)
{
Console.WriteLine("The value {0}, Is not valid", txtScore2.Text);
}
catch (Exception exception)
{
Console.WriteLine("Unexpected Error : {0}", exception.Message);
}
finally
{
Console.WriteLine("Thanks");
}
try
{
testScores[2] = int.Parse(txtScore3.Text);
}
catch (FormatException)
{
Console.WriteLine("The value {0}, Is not valid", txtScore3.Text);
}
catch (Exception exception)
{
Console.WriteLine("Unexpected Error : {0}", exception.Message);
}
finally
{
Console.WriteLine("Thanks");
}
}
float calcGrades(){
float grade;
grade = (float)(testScores[0] + testScores[1] + testScores[2] / 3);
return grade;
}
void displayGrade(float grade) {
// checks conditions(expressions);if true executes code and exit
if (grade > 90) { //is greater than 90
lblAverage.Text = "Your score is " + " " + grade + "<br /> well done ! ";
}
if (grade > 80) { //is greater than 80
lblAverage.Text = "Your is score" + " " + grade + "<br /> Nice one ! ";
}
else { // if two expression not true just do this
lblAverage.Text = "Your score is " + " " + grade;
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// calls the initGrades method
initGrades();
// class the displayGrade and passes in parameter from return value of calcGrades
displayGrade(calcGrades());
}
}
Br
Henry
Reply
Answers (
1
)
How to make individual cell of gridview editable?
MVC pattern