Vb.net 除法和对数在计算器中的应用
我正在vb.net中创建一个计算器。我有两个问题。Vb.net 除法和对数在计算器中的应用,vb.net,calculator,Vb.net,Calculator,我正在vb.net中创建一个计算器。我有两个问题。 1) 我已经像这样处理过零除法 Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal Dim output As Decimal firstNum = num1 secondNum = num2 Select Case
1) 我已经像这样处理过零除法
Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
Dim output As Decimal
firstNum = num1
secondNum = num2
Select Case inputOp
Case "+"
output = num1 + num2
Case "-"
output = num1 - num2
Case "/"
Dim value As Decimal
Try
isFirst()
value = (num1 / num2)
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
output = value
Case "*"
output = num1 * num2
Case "Mod"
output = (num1 Mod num2)
Case "^"
output = CDec(Math.Pow(num1, num2))
End Select
Return output
End Function
和对数值一般来说,当程序的运行部分无法获得有意义或正确的结果时,您应该抛出异常,并且您希望强制程序的更高部分处理出错的内容,以便它能够合理地继续 Log10方法总是返回一个Double,这个Double有一个方便的“特殊”返回值NaN,这意味着不是一个数字 如果他们没有返回NaN的选项,那么.NET framework创建者可能抛出了某种异常。然而,这使得使用Log10的开发人员在每次调用Log10时都必须检查NaN,而不能使用try/catch块 NET中有几个专门的异常类。如果您希望在传入负数时从代码中抛出异常,那么在这种情况下,可以选择ArgumentException
If firstNum <= 0 Then
Throw New ArgumentException("Can't calculate the logarithm of a negative number")
If firstNum感谢您的回答。使用Try Catch和Throw有什么区别。这就是编写为Try txtCalc.Text=Math.Log10(CType(firstNum,Double)).ToString()isFirstExist=False Catch ex as ArgumentException MessageBox.Show的区别(“无法计算负数的对数”,“错误”,MessageBoxButtons.OK)结束Try
并以的形式写入,如果firstNum,则所有内容都是关于要在何处处理异常。如果要在当前函数中处理异常,请使用Try-catch块。如果您认为在其他位置(程序调用堆栈的更高位置)处理异常更有意义,则不能使用try/catch(在这种情况下,将自动引发任何异常),或者如果您决定强制程序的其他部分处理异常,请使用try/catch,然后在catch中抛出异常。谢谢。因此,try-catch和throws是处理异常的两种方法。请明确:try-catch是处理异常的一种方法。throw是强制更高级别的内容来处理异常的方法(或者,如果什么都不做,则使程序崩溃)。
If firstNum <= 0 Then
Throw New ArgumentException("Can't calculate the logarithm of a negative number")