Reputation: 747
I am creating a calculator in vb.net.I have two questions.
1)I have handled division by zero like this.
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
And logarithm values <=0 as this :
Private Sub btn_log_Click(sender As System.Object, e As System.EventArgs) Handles btn_log.Click
isFirst()
If firstNum <= 0 Then
txtCalc.Text = "Can't calculate the logaritm of a negative number"
Else
txtCalc.Text = Math.Log10(CType(firstNum, Double)).ToString()
isFirstExist = False
End If
End Sub
My question is what's the difference in printing out a message as in log and having an exception thrown as in division case.
For the log also do I have to have an exception.If so can someone point out how I can do this because i don't think there's an exception as DivideByZeroException
for negative logarithms.
My second question is when I take the logarithm of a negative value it does print out the line "Can't calculate the logarithm of a negative number"
but the font size is too big.Hw can I make the font of this line printed out to be small?
Upvotes: 0
Views: 286
Reputation: 4647
In general, you should throw an exception when there is no way for the part of the program that is running to proceed with meaningful or correct results, and you want to force a higher part of the program to handle what went wrong so it can proceed reasonably.
The Math.Log10 method always returns a Double which has a somewhat handy "special" return value of NaN, which means Not a Number.
http://msdn.microsoft.com/en-us/library/system.math.log10(v=vs.110).aspx
If they didn't have the option to return NaN, then feasibly the .NET framework creators might have thrown some sort of exception. However this puts developers who use Log10 in a position that they have to check for NaN every time they call Log10 rather than being able to use a try/catch block.
There are several specialized Exception classes in .NET. If you wanted to throw an exception from your code if a negative number was passed in, a good one to choose in this case might be the ArgumentException.
If firstNum <= 0 Then
Throw New ArgumentException("Can't calculate the logarithm of a negative number")
But your solution of printing out an error to your log is probably better in this case since the entire point of an exception is "forcing something higher to deal with it". Your program doesn't have anything higher than the button click handler, so throwing an exception would probably cause it to crash with an "unhandled exception".
Regarding the font issue, there is a property on the TextFox control that lets you set the font face, size, and style. Select it in the designer and dig around in the properties window for it. If you want just your errors to be big and other results to be small, you may need to use some sort of rich textbox that supports inline styles.
Upvotes: 1