0

试图在计算器中捕捉除以零的异常并且它不起作用,不确定我哪里出错了?

Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click

    If lblOperator.Text = "+" Then
        txtAnswer.Text = Val(txtNumberOne.Text) + Val(txtNumberTwo.Text)
    ElseIf lblOperator.Text = "-" Then
        txtAnswer.Text = Val(txtNumberOne.Text) - Val(txtNumberTwo.Text)
    ElseIf lblOperator.Text = "x" Then
        txtAnswer.Text = Val(txtNumberOne.Text) * Val(txtNumberTwo.Text)
    ElseIf lblOperator.Text = "/" Then
        Try
            txtAnswer.Text = Val(txtNumberOne.Text) / Val(txtNumberTwo.Text)
        Catch ex As Exception When Val(txtNumberTwo.Text = 0)
            MessageBox.Show("You cannot divdide by zero")
        End Try
    End If

End Sub
4

1 回答 1

0

用浮点数除以零返回 Infinity、-Infinity 或 NaN,而不是引发异常。请参阅文档:http: //msdn.microsoft.com/en-us/library/25bswc76.aspx

于 2013-03-31T17:46:23.353 回答