|
-
Feb 7th, 2004, 11:16 PM
#1
Thread Starter
New Member
VB.net 2003 system.overflowexecption
I have a basic math calculator program. This application was designed to perform basic math calculations: +, -, *, /. The program was provided for us and we have to fix the errors in it. I have to keep the varaibles declared as short.
1. Test all operators with the values 8 and 2 to make sure it works as expected.
2. Test the program with 99999 + 99999. This should raise a 'System.OverflowException’ exception.
3. Modify the code to catch and handle the exception so that the user can continue. KEEP the declarations of FirstNum and SecondNum as short.
When I do 3 I am still getting the error code. I am placing a breakpoint at the highlighted area. The breakpoint says " this code has called into another function. When that function is finished this is the next statement that will be executed" It is placing this next to the Firstnum = Textbox.text HEre is the code:
'Declare FirstNum and SecondNum variables as global
Dim FirstNum, SecondNum As Short
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Assign text box values to variables
FirstNum = TextBox1.Text
SecondNum = TextBox2.Text
Try
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum + SecondNum
End If
Catch Exc As System.OverflowException
MessageBox.Show("Please choose smaller number")
End Try
'Determine checked button and calculate
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
End If
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
End If
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
End If
End Sub
End Class
Any assistance would be greatly appreciated
-
Feb 8th, 2004, 12:20 AM
#2
Frenzied Member
-
Feb 8th, 2004, 12:47 AM
#3
Thread Starter
New Member
Thanks for the response. I apoligize for the mess up and have asked one of the moderators to move this to the vb.net forum.
-
Feb 8th, 2004, 04:28 AM
#4
Please use [ vbcode ] [ /vbcode ] w/o spaces between code
VB Code:
'Declare FirstNum and SecondNum variables as global
Dim FirstNum, SecondNum As Short
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Assign text box values to variables
FirstNum = TextBox1.Text
SecondNum = TextBox2.Text
Try
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum + SecondNum
End If
Catch Exc As System.OverflowException
MessageBox.Show("Please choose smaller number")
End Try
'Determine checked button and calculate
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
End If
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
End If
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
End If
End Sub
VB Code:
'Declare FirstNum and SecondNum variables as global
Dim FirstNum, SecondNum As Short
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Assign text box values to variables
FirstNum = TextBox1.Text
SecondNum = TextBox2.Text
Try
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum + SecondNum
Exit Sub ' not really needed but reduces
'work(ever-so slightly)
End If
'Determine checked button and calculate
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
Exit Sub
End If
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
Exit Sub
End If
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
Exit Sub
End If
Catch Exc As System.OverflowException
MessageBox.Show("Please choose smaller number")
End Try
End Sub
you didnt apply the Try/Catch statement for all of your operations (unless i missed something???)
Im pretty sure that this was your problem but i was too lazy to create all the controls and test it out
I would also watch for a System.DivideByZeroException
you will also have problems if the user enters text or a space by accident
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Feb 8th, 2004, 08:20 AM
#5
Thread Starter
New Member
Thanks for the effort however it is still giving the same error. I will repost in VB.net and see what I can find.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|