1

我想在我正在创建的模块上添加“try catch”语句。我想指定以数字格式捕获的异常。这是示例代码,

    Try
       interest = me.txtInterest.text
       principal = me.txtPrincipal.text
       totalPayment = interest + principal
    Catch ex As Exception 'What is the proper exception for Number Format?
        MsgBox("Number Format Error")
    End Try

我想指定数字格式的例外。我怎么能那样做?

4

2 回答 2

2

使用此示例代码

   Try
            Dim no1 As Integer = Int16.Parse(Me.TextBox1.Text)
            Dim no2 As Integer = Int16.Parse(Me.TextBox2.Text)
            Dim toatlPayment As Integer = no1 + no2


        Catch ex As FormatException
            MessageBox.Show(ex.Message)

        End Try
于 2013-02-21T12:03:56.850 回答
0

您可以检查它是否是有效数字,而不是捕获异常

If Int32.TryParse(me.txtInterest.text, interest) AndAlso Int32.TryParse(me.txtPrincipal.text, principal) Then
    totalPayment = interest + principal
Else
    MsgBox("Number Format Error")
End If
于 2013-02-21T18:14:16.577 回答