【发布时间】:2013-11-12 06:01:42
【问题描述】:
我有这个代码。我在 3 个不同的文本框中输入三个成绩,然后单击在列表框中显示成绩的提交按钮。除了这个,效果很好。当我只输入一个或两个等级并单击提交时,我收到错误消息“异常未处理”。我将如何编码以防止这种情况发生。错误发生在这里。
' retrieve the student's grades
grades(studentCount, 0) = Convert.ToInt32(test1TextBox.Text)
grades(studentCount, 1) = Convert.ToInt32(test2TextBox.Text)
grades(studentCount, 2) = Convert.ToInt32(test3TextBox.Text)
Private Sub submitButton_Click(sender As Object,
e As EventArgs) Handles submitButton.Click
' process one student's grades
' retrieve the student's grades
grades(studentCount, 0) = Convert.ToInt32(test1TextBox.Text)
grades(studentCount, 1) = Convert.ToInt32(test2TextBox.Text)
grades(studentCount, 2) = Convert.ToInt32(test3TextBox.Text)
' begin creating String containing the student's grades and average
Dim output As String = "Student " & studentCount & vbTab
' append each test grade to the output
For column = 0 To grades.GetUpperBound(1)
' if the Letter RadioButton is checked
If letterRadioButton.Checked = True Then
' append letter grade to the output
output &= vbTab & LetterGrade(grades(studentCount, column))
Else
' append number grade to the output
output &= vbTab & grades(studentCount, column)
End If
Next
' append the student's test average to the output
output &= vbTab & CalculateStudentAverage(studentCount)
gradesListBox.Items.Add(output) ' add output to the ListBox
studentCount += 1 ' update number of students entered
lblClassAverageResult.Text = CalculateClassAverage() ' display class average
DisplayBarChart() ' display the current grade distribution
' clear the input TextBoxes and set focus to first TextBox
test1TextBox.Clear()
test2TextBox.Clear()
test3TextBox.Clear()
test1TextBox.Focus()
' limit number of students
If studentCount = grades.GetUpperBound(0) + 1 Then
inputGradesGroupBox.Enabled = False ' disable GroupBox's controls
End If
End Sub ' submitButton_Click
【问题讨论】:
标签: .net vb.net error-handling try-catch