如何处理VB.NET程序中由无效用户输入引起的错误?

时间:2011-03-10 08:48:09

标签: vb.net error-handling

我是VB.NET的初学者,我想知道如何处理程序用户可能出现的错误。例如,我有一个三角形区域程序,我要求程序的用户在文本框中插入三角形的基部长度和高度。但是如果他插入一封信或其他东西,比如一个标志......我的程序会崩溃......

我知道一种方法是使用 On Error GoTo ,但我不知道如何。如果你可以指导我一些链接,或者你可以解释我如何处理错误,我将不胜感激。也许你甚至可以告诉我其他方法来避免用户输入的错误。

到目前为止,这是我的小程序:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
                Handles Button1.Click

    Dim , Height, ARIA As Integer

    Base = TextBox1.Text
    Height = TextBox2.Text
    ARIA =  Base * Height / 2
    TextBox3.Text = ARIA

End Sub

3 个答案:

答案 0 :(得分:3)

您需要做的是:

Try
    'Do something dangerous
Catch ex As System.Exception 'catch any error
    'Handle the error here
End Try

如果您有任何需要执行的“清理”任务,无论是否发生任何错误(例如关闭文件),请执行以下操作:

Try
    'The dangerous code here will get executed
Finally
    'Whether the code throws any exception or not, this part will ALWAYS run
End Try
'If there was any error, then it would be thrown AFTER the end of the 'finally'

您还可以拥有更复杂的异常处理程序,例如:

Try
    'Dangerous code
Catch ex As FormatException
    'This code catches only a FormatException
Catch ex As ArithmeticException
    'This code catches only ArithmeticException
'Any other kind of error is NOT caught
Finally
    'Regardless of whether the error was handled, this part will execute
End Try
'Unhandled exceptions will be thrown before the block continues

答案 1 :(得分:2)

除了其他人已经编写的内容之外,我认为使用“Visual Basic最佳实践”查看代码的外观可能会有所帮助。您可能尚未准备好所有这些,但也许您会发现一些有用的概念:

' This is at the top of the file and forces us to do explicit instead
' of implicit conversions. You can also set this in the project settings
Option Strict On    

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Handles Button1.Click

    ' It's convention to have local variables start with a lower-case character
    Dim base, height, aria As Integer 

    Try
        ' Since Option Strict is on, we need to explicitly convert
        ' Strings to Integers.
        base = Integer.Parse(TextBox1.Text) 
        height = Integer.Parse(TextBox2.Text)

    ' The documentation of Int32.Parse (Integer is a Visual Basic alias for
    ' Int32) says that a FormatException and an OverflowException can occur,
    ' so we catch and handle them here.
    Catch ex As FormatException
        MsgBox("Base or Height is not a number")
        Return   ' Leave the sub '

    Catch ex As OverflowException
        MsgBox("Base or Height is too large")
        Return
    End Try

    aria = base * height / 2
    TextBox3.Text = aria.ToString()    ' Convert the Integer back into a String

End Sub

或者,您可以使用TryParse将Integer转换为String。它不会抛出异常,而是在转换失败时返回false:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            Handles Button1.Click

    Dim base, height, aria As Integer 

    If Integer.TryParse(TextBox1.Text, base) = False OrElse Integer.TryParse(TextBox2.Text, height) = False Then
        MsgBox("Base or Height is invalid")
        Return
    End If

    aria = base * height / 2
    TextBox3.Text = aria.ToString()
End Sub

这样,你完全避免了异常处理(在这种情况下)。

答案 2 :(得分:1)

你的作业应该是这样的......

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim Base, Height, ARIA As Integer
    Try
        Base = TextBox1.Text
        Height = TextBox2.Text
        ARIA = Base * Height / 2
        TextBox3.Text = ARIA
    Catch ex As Exception
        MessageBox.Show("Please enter a number")
    End Try
End Sub