【问题标题】:How do I code against an unhandled exception如何针对未处理的异常进行编码
【发布时间】: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


    【解决方案1】:

    我认为您的 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
    

    【讨论】:

    • 在消息框上单击“确定”后,顶级场景仍然将零解析到列表框,您可以通过某种方式清除所有内容吗
    • @JasonSmith 我意识到,我只是添加它以显示您可以放置​​一些条件逻辑来处理错误,当文本框中没有输入文本时您具体想要做什么
    • 我不想解析任何东西。想清除文本框并使用输入成绩编号,然后输入下一个文本框成绩,直到所有三个文本框都有成绩。
    • @JasonSmith 仅在他们单击确定时显示您的 MessageBox,然后清除 TextBoxes 并使用 Exit Sub 退出您的 Click EventHandler
    • @JasonSmith 刚刚编辑了答案以显示我在说什么
    【解决方案2】:

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

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-26
      • 2019-08-06
      • 2020-01-20
      相关资源
      最近更新 更多