Results 1 to 5 of 5

Thread: VB.net 2003 system.overflowexecption

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    11

    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

  2. #2
    Frenzied Member
    Join Date
    May 2003
    Location
    So Cal
    Posts
    1,564
    Try the .Net forum

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    11
    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.

  4. #4
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    Please use [ vbcode ] [ /vbcode ] w/o spaces between code

    VB Code:
    1. 'Declare FirstNum and SecondNum variables as global
    2.     Dim FirstNum, SecondNum As Short
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         'Assign text box values to variables
    6.         FirstNum = TextBox1.Text
    7.         SecondNum = TextBox2.Text
    8.         Try
    9.             If RadioButton1.Checked = True Then
    10.                 TextBox3.Text = FirstNum + SecondNum
    11.             End If
    12.         Catch Exc As System.OverflowException
    13.             MessageBox.Show("Please choose smaller number")
    14.         End Try
    15.         'Determine checked button and calculate
    16.  
    17.         If RadioButton2.Checked = True Then
    18.             TextBox3.Text = FirstNum - SecondNum
    19.         End If
    20.         If RadioButton3.Checked = True Then
    21.             TextBox3.Text = FirstNum * SecondNum
    22.         End If
    23.         If RadioButton4.Checked = True Then
    24.             TextBox3.Text = FirstNum / SecondNum
    25.         End If
    26.  
    27.     End Sub

    VB Code:
    1. 'Declare FirstNum and SecondNum variables as global
    2.     Dim FirstNum, SecondNum As Short
    3.  
    4.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.         'Assign text box values to variables
    6.         FirstNum = TextBox1.Text
    7.         SecondNum = TextBox2.Text
    8.  
    9.         Try
    10.             If RadioButton1.Checked = True Then
    11.                 TextBox3.Text = FirstNum + SecondNum
    12.                 Exit Sub ' not really needed but reduces
    13.                 'work(ever-so slightly)
    14.             End If
    15.             'Determine checked button and calculate
    16.  
    17.             If RadioButton2.Checked = True Then
    18.                 TextBox3.Text = FirstNum - SecondNum
    19.                 Exit Sub
    20.             End If
    21.  
    22.             If RadioButton3.Checked = True Then
    23.                 TextBox3.Text = FirstNum * SecondNum
    24.                 Exit Sub
    25.             End If
    26.  
    27.             If RadioButton4.Checked = True Then
    28.                 TextBox3.Text = FirstNum / SecondNum
    29.                 Exit Sub
    30.             End If
    31.  
    32.         Catch Exc As System.OverflowException
    33.             MessageBox.Show("Please choose smaller number")
    34.         End Try
    35.  
    36.     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

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2004
    Posts
    11
    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
  •  



Click Here to Expand Forum to Full Width