如何针对未处理的异常进行编码

时间:2013-11-01 02:35:48

标签: .net vb.net error-handling try-catch

我有这个代码。我在3个不同的文本框中输入三个等级,然后单击一个在列表框中显示成绩的提交按钮。除此之外,效果很好。当我只输入一个或两个成绩并单击提交时,我收到错误消息“Exception was unhandeled”。我将如何编码以防止这种情况发生。错误发生在这里。

 ' 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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我假设您的Convert.ToInt32语句发生了错误,请尝试使用Int32.TryParse。您可以使用Try / Catch / Finally语句来捕获您遇到的任何错误,但我认为更好的方法是编写程序中常见的错误并使用Try / Catch例程来解决异常错误。< / p>

从上面链接:

  

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