2

I have this code. I enter three grades into 3 different textbox and then click a submit button that displays the grades in a listbox. Works well except for this. When I enter one or two grades only and click submit I get error message "Exception was unhandeled". How would I code this to prevent this from happening. The Error Happens here.

 ' 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
4

2 回答 2

0

它被称为错误处理。这是一个例子:

Try
    Process.Start("http://www.microsoft.com")
Catch ex As Exception
    MsgBox("Can't load Web page" & vbCrLf & ex.Message)
End Try

Try基本上,您可以在“ ”和“ Catch”语句之间放置任何您怀疑它可能会给您带来例外的东西。还有一个Finally说法。继续阅读以了解有关错误处理的更多信息。

http://msdn.microsoft.com/en-us/library/s6da8809(v=vs.90).aspx

http://msdn.microsoft.com/en-us/library/fk6t46tz.aspx

于 2013-11-01T02:41:53.387 回答
0

我认为您的 Convert.ToInt32 语句正在发生错误,请尝试使用Int32.TryParse。您可以使用 Try/Catch/Finally 语句来捕获您遇到的任何错误,但在我看来,更好的方法是围绕程序中常见的错误进行编码,并使用 Try/Catch 例程来处理异常错误。

从上面的链接:

TryParse 方法与 Parse 方法类似,只是 TryParse 方法在转换失败时不会抛出异常。它消除了在 s 无效且无法成功解析的情况下使用异常处理来测试 FormatException 的需要。

如果您想在其中一个字段未填写时显示错误,您可以执行类似的操作。

Dim grade1, grade2, grade3 As Int32
If Int32.TryParse(test1TextBox.Text, grade1) Then
    grades(studentCount, 0) = grade1
Else
    'Message Box here
End If

If Int32.TryParse(test2TextBox.Text, grade2) Then
    grades(studentCount, 1) = grade2
Else
        'Message Box here
End If

If Int32.TryParse(test3TextBox.Text, grade3) Then
    grades(studentCount, 2) = grade3
Else
    'Message Box here
End If

或者,如果您只想解析它并且不关心零数量,您可以执行以下操作:

Dim grade1, grade2, grade3 As Integer
Int32.TryParse(test1TextBox.Text, grade1)
Int32.TryParse(test2TextBox.Text, grade2)
Int32.TryParse(test3TextBox.Text, grade3)

grades(studentCount, 0) = grade1
grades(studentCount, 1) = grade2
grades(studentCount, 2) = grade3

根据评论编辑

If Integer.TryParse(test1TextBox.Text, grade1) Then
    grades(studentCount, 0) = grade1
Else
    HandleInputError(0) 'This is a Subroutine with common code  in it for showing MessageBox and Clearing text
    Exit Sub
End If

HandleInputError 子程序

Sub HandleInputError(grade As Integer)
    MsgBox("Please Re-Enter Grade #" & (grade + 1).ToString, vbExclamation, vbOKOnly)
    Select Case grade
        Case 0
            test1TextBox.Text = ""
        Case 1
            test2TextBox.Text = ""
        Case 2
            test3TextBox.Text = ""
    End Select
End Sub
于 2013-11-01T02:44:38.763 回答