Posted on 2004-10-01
I am a college student and this is an assignment I have read the other posts and have tried using some of the code but am having trouble getting it. Below is the requirements of the assignment and the code I have so far any help getting the try catch statements to work would be helpful and appreciated! Basically I’m lost here not looking for the specific answer but some guidance.
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.
4. Test the program with ‘9 divide 0’. The result shows ‘infinity’. Actually, the calculation should raise an exception ‘System.DivideByZeroException’. Modify the program so that the user input is validated before the divide operation is performed. For a divide by zero, modify the program to raise an exception, then add the code to handle the exception.
5. Try the test (99 * 999). You should get a 'System.OverflowException’ exception. Fix the problem to handle the exception.
6. Now test the '-' operator using the following test cases: (1) variable1 = 32000 and variable2 = -767 and (2) variable1 = 32000 and variable2 = -768. For the first test case, the result should be 32767. For the second test case, you should get an exception. Explain what is different from the first case and fix the problem to handle the exception.
7. Make sure to create a user-friendly interface to guide the user after the exceptions are handled.
'Declare FirstNum and SecondNum variables
Dim FirstNum, SecondNum As Short
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
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
'Determine checked button and calculate
If RadioButton1.Checked = True Then
'this is my first try catch not much happening here except an error from the compiler
TextBox3.Text = FirstNum + SecondNum
Catch Exc As System.OverflowException
MessageBox.Show("Please choose smaller number")
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
If RadioButton4.Checked = True Then
'here is another example that I have tried but no clue why it isn't functioning
TextBox3.Text = FirstNum / SecondNum
Catch ex As DivideByZeroException
MessageBox.Show("Division by Zero caused an error!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch ex As Exception
MessageBox.Show(ex.Message, "There has been an error", MessageBoxButtons.OK, MessageBoxIcon.Error)