locked
Try...Catch Exceptions not working with my program RRS feed

  • Question

  • I'm getting so frustrated. I'm new to Visual Studio 2005 and to Visual Basic. I had to create a program that you could put a number in TextBox1 and a number in TextBox2 and depending on which formula you want to use either the Pythagorean Theorum or the Cylinder Volume formula (by selecting a RadioButton) it will give you the results. Well I got the results working so that I could put the numbers in and the results would be accurate.

    Then the next step was to develop try...catch exceptions to handle any error that can be typed by the user. Including but not limited to, overflow exceptions, type mismatch, and division by zero, and an appropriate message needs to display with the error. Well since I already had the equations working right I added how I thought the Try...Catch would be done and then I ran the Debug and I input a letter and the right error message popped up. Sounds good right?

    Well I ran it again and put numbers into the appropriate Text Boxes, Hit Calculate, and nothing happened. My dilemma is that if I have the Try...Catch exception in there, only the error messages work correctly and not the calculations that should work. If I don't have the Try...Catch exception in there the calculations work correctly. For example, say I'm trying to get the answer to the Pythagorean Theorum (a^2 + b^2 = c^2) and I put 1 in box 1 and 1 in box 2 (and I don't have the Try...Catch exceptions in the code) I will select Calculate and the correct answer of 4 (2^2) will show up in the Results box. But if I put the Try...Catch exception in there when I hit Calculate it doesn't calculate unless I put something in there that will cause an error to pop up like if I put &#*& in box 1 it will cause the mismatch error to pop up, but if I clear the boxes and put a 1 in box 1 and a 1 in box 2 and hit calculate nothing happens.

    My main problem is I can't find anything that says where I'm supposed to put the Try...Catch exception coding or if I do have Try...Catch exceptions where it goes if there is a working calculation already in the code. I don't even know if I'm doing this right. Is there anyone that can help me??

    Here's the code that works without the Try...Catch Exceptions:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'Determine checked button and calculate

    If RadioButton1.Checked = True Then

    TextBox3.Text = (TextBox1.Text ^ 2 + TextBox2.Text ^ 2)

    TextBox3.Text = TextBox3.Text ^ 2

    Else

    If RadioButton2.Checked = True Then

    TextBox3.Text = (Math.PI * TextBox1.Text ^ 2 * TextBox2.Text)

    End If

    End If

    End Sub

    Here's what I have with the Try...Catch exception:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim number1, number2, results As Decimal

    Try

    number1 = Me.TextBox1.Text

    number2 = Me.TextBox2.Text

    results = Me.TextBox3.Text

    'Determine checked button and calculate

    If RadioButton1.Checked = True Then

    results = (number1 ^ 2 + number2 ^ 2)

    results = results ^ 2

    Else

    If RadioButton2.Checked = True Then

    results = (Math.PI * number1 ^ 2 * number2)

    End If

    End If

    Catch When Err.Number = 13 'Type mismatch

    MsgBox("Check Box 1 or Box 2 to ensure only a number was typed in!")

    Catch When Err.Number = 11 'Division by zero

    MsgBox("Division by zero not possible choose another number")

    Catch When Err.Number = 6 'Overflow Error

    MsgBox("Input has created an overflow error, please input a smaller number")

    End Try

    End Sub

    If anyone can help me with this even a little help, it'd be greatly appreciated.

    Nicole


    • Edited by packer01fan Sunday, July 13, 2008 3:50 PM Its has two separate sections of code in it.
    Sunday, July 13, 2008 3:39 PM

Answers

  • Looks like you need a TextBox3.Text = results in there.

    Immediately after the following, before the Catch:

    If RadioButton2.Checked = True Then 
      results = (Math.PI * number1 ^ 2 * number2)  
    End If 
     
    End If 
     
    textBox3.Text = results 



     

    • Proposed as answer by Calle Mellergardh Monday, July 14, 2008 9:50 AM
    • Marked as answer by nobugz Monday, July 14, 2008 11:27 AM
    Monday, July 14, 2008 1:22 AM

All replies

  • Looks like you need a TextBox3.Text = results in there.

    Immediately after the following, before the Catch:

    If RadioButton2.Checked = True Then 
      results = (Math.PI * number1 ^ 2 * number2)  
    End If 
     
    End If 
     
    textBox3.Text = results 



     

    • Proposed as answer by Calle Mellergardh Monday, July 14, 2008 9:50 AM
    • Marked as answer by nobugz Monday, July 14, 2008 11:27 AM
    Monday, July 14, 2008 1:22 AM
  • Please turn Option Strict On before posting VB code.  Since running your code could damage a responders computer, few people will take a chance without the protection that Option Strict On provides. 
    Monday, July 14, 2008 5:43 AM
  • Oh I didn't know. I've never posted on here before. I apologize.

    V/R

    Nicole
    Monday, July 14, 2008 8:30 AM
  • Try to use Decimal.TryParse() to solve both your exception handling dilemma and your typing problem.  Another approach is to use the Validating event.  It ensures the user cannot enter an invalid number:

      Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        Dim number As Decimal
        If Not Decimal.TryParse(TextBox1.Text, number) Then
          MessageBox.Show("Please enter a valid number")
          e.Cancel = True
        End If
      End Sub


    Hans Passant.
    Monday, July 14, 2008 11:26 AM
  • Thank you for the tip on the Decimal Parse. I also want to thank BinaryCoder. It worked like a charm. Hard to believe that simple line caused me 5 hours of pain and agony. Thank you so much.
    Monday, July 14, 2008 6:42 PM