supercoqui
asked on
use try...catch alone to input numbers less than 19 digits long in text box
Using try..catch statement alone i am trying to input a number in a text box. I am trying to make it so that a message pops if the input is 19 9s or more. I dont want the answers textbox to display infinity, instead i want it to pop a msgbox saying the input is too big. Here is the code: (please try not to use if...else statements, only try...catch)
'defines what happens when the user clicks on the calculate button. It displays the equation on the text box for result.
Private Sub CALCULATEButton_Click(ByVa l sender As System.Object, ByVal e As System.EventArgs) Handles CALCULATEButton.Click
Try
IsNumeric(radius)
'assigns the value of the RADIUS text box to the radius variable
radius = RADIUSTextBox.Text
'Determine what radio buttons are checked and calculate according
If SURFACERadioButton.Checked = True Then
equation = 4 * PI * (radius ^ 2)
End If
If VOLUMERadioButton.Checked = True Then
equation = (4 / 3) * PI * (radius ^ 3)
End If
RESULT = CStr(equation)
'displays the result of the equation
RESULTTextBox.Text = equation
Catch ex As OverflowException
Console.WriteLine("Error: Input too large/small, Calculated value too large/small")
MsgBox("Error: Input too large/small, Calculated value too large/small" & vbCrLf & "Please enter a different value", , "Sphere Volume and Area Calculator Version 2")
RADIUSTextBox.Text = 1
Catch ex As InvalidCastException
Console.WriteLine("Error: incorrect input, string used")
MsgBox("Please enter a valid number!" & vbCrLf & "Expressions like a , ! @ # $ % ^ & * ( ) _ + = | / \ { } [ ] ' : ; < > ?" & vbCrLf & "are not accepted.", , "Sphere Volume and Area Calculator Version 2")
RADIUSTextBox.Text = 1
End Try
End Sub
'defines what happens when the user clicks on the calculate button. It displays the equation on the text box for result.
Private Sub CALCULATEButton_Click(ByVa
Try
IsNumeric(radius)
'assigns the value of the RADIUS text box to the radius variable
radius = RADIUSTextBox.Text
'Determine what radio buttons are checked and calculate according
If SURFACERadioButton.Checked
equation = 4 * PI * (radius ^ 2)
End If
If VOLUMERadioButton.Checked = True Then
equation = (4 / 3) * PI * (radius ^ 3)
End If
RESULT = CStr(equation)
'displays the result of the equation
RESULTTextBox.Text = equation
Catch ex As OverflowException
Console.WriteLine("Error: Input too large/small, Calculated value too large/small")
MsgBox("Error: Input too large/small, Calculated value too large/small" & vbCrLf & "Please enter a different value", , "Sphere Volume and Area Calculator Version 2")
RADIUSTextBox.Text = 1
Catch ex As InvalidCastException
Console.WriteLine("Error: incorrect input, string used")
MsgBox("Please enter a valid number!" & vbCrLf & "Expressions like a , ! @ # $ % ^ & * ( ) _ + = | / \ { } [ ] ' : ; < > ?" & vbCrLf & "are not accepted.", , "Sphere Volume and Area Calculator Version 2")
RADIUSTextBox.Text = 1
End Try
End Sub
btw, this is in C#, should be easy to convert to vb.net
ASKER
wow, almost works, it blocks on 10 9s and there after, but i can still get values with 18 9s without using the statement provided. My normal code gives the infinity at 19 9s, so i am trying for it to work normally if i input 18 9s.
I only used this part because the rest gave me error:
Convert.ToInt32(RADIUSText Box.Text)
I only used this part because the rest gave me error:
Convert.ToInt32(RADIUSText
ASKER
ok I changed the value to
Convert.ToInt64(RADIUSText Box.Text)
and it works fine for the surface, on 19 9s it catched the exception.
Now the problem lies when i do it for volume, volume only works with 12 9s and then it goes into displaying infinity.
Convert.ToInt64(RADIUSText
and it works fine for the surface, on 19 9s it catched the exception.
Now the problem lies when i do it for volume, volume only works with 12 9s and then it goes into displaying infinity.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found the way messing around with the information provided by bramsquad, thanks
hmm, 12 9's is weird because even an UInt32 is less than 10 9's
are you doing the same checked statement? something like
checked { Convert.ToInt64(VOLUMEText Box.Text); }
???
are you doing the same checked statement? something like
checked { Convert.ToInt64(VOLUMEText
???
well done
for example
try
{
checked { Convert.ToInt32(RADIUSText
}
catch (OverflowException e)
{
Console.WriteLine("Error caught: {0}", e);
}
~b