|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I'm Doing a simple Math calculator but I want to include the
following items For the sume When I test the program with 99999 + 99999. This should raise a 'System.OverflowException’ exception. I want to modify the code to catch and handle the exception so that the user can continue.I want to keep the declarations of FirstNum and SecondNum as short. For the Division When I test the program with ‘9 divide 0’. The result shows ‘infinity’. Actually, I want the calculation should raise an exception ‘System.DivideByZeroException’.Also I want to modify the program so that the user input is validated before the divide operation is performed. For a divide by zero, I want to modify the program to raise an exception, then add the code to handle the exception. For the multiplication When I perform the test on (99 * 999). I got a 'System.OverflowException’ exception. I want to Fix the problem to handle the exception. Now when I 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 and I want to find a solution Here is my code: Code:
Public Class Form1
Inherits System.Windows.Forms.Form
Dim FirstNum, SecondNum As Short
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Assign text box values to variables
FirstNum = TextBox1.Text
SecondNum = TextBox2.Text
'Determine checked button and calculate
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum + SecondNum
End If
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
End If
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
End If
Try ' I try this to see if works but don't work
Err.Raise(11)
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
End If
Catch When Err.Number = 11
MsgBox("Don't Divide 9/0")
End Try
End Sub
Can any body help me? Thanks zorro23 Last edited by nofriends : March 10th, 2006 at 01:26 AM. Reason: added code blocks |
|
#2
|
||||
|
||||
|
Hi, why not add the try catch block around the whole section
of code, this way it will raise the error for each one of them try this Code:
'Determine checked button and calculate
Try ' Put it here and see what happens
If RadioButton1.Checked = True Then
TextBox3.Text = FirstNum + SecondNum
End If
If RadioButton2.Checked = True Then
TextBox3.Text = FirstNum - SecondNum
End If
If RadioButton3.Checked = True Then
TextBox3.Text = FirstNum * SecondNum
End If
Err.Raise(11)
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
End If
Catch When Err.Number = 11
MsgBox("Don't Divide 9/0")
End Try
hope this helps
__________________
Look! Its a ShemZilla ![]() ![]()
|
|
#3
|
||||
|
||||
|
as far as I know, Try..Catch is not supposed to catch error manually raised with Err.Raise...
just have code like nofriends posted and test it by giving big numbers. |
|
#4
|
||||
|
||||
|
I just use...
Code:
Private Sub Blah()
On Error Goto ItMessedUp
Blah Blah Blah Blah
Blah Blah Blah Blah
Blah Blah Blah Blah
Blah Blah Blah Blah
Code that generates an error here
Blah Blah Blah Blah
Blah Blah Blah Blah
Blah Blah Blah Blah
Exit Sub
ItMessedUp:
msgbox "Your program messed up!" & vbcrlf & _
"If you really want the details, here they are..." & vbcrlf & _
err.Description
end sub
__________________
LozWare Website Directory Whooo! Free submissions, no recip needed. I'm a nice guy
Last edited by LozWare : March 10th, 2006 at 06:44 PM. |
|
#5
|
|||
|
|||
|
I need a little push on Try...Catch
Since I got many ideas from all of you I make the code:
__________________________________________________ ______________ Dim FirstNum,Second As Short __________________________________________________ ______________ PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try FirstNum = TextBox1.Text SecondNum = TextBox2.Text If RadioButton1.Checked = TrueThen------>This work fine TextBox3.Text = FirstNum + SecondNum EndIf If RadioButton2.Checked = TrueThen------>This work fine TextBox3.Text = FirstNum - SecondNum EndIf If RadioButton3.Checked = TrueThen----->This Work Fine TextBox3.Text = FirstNum * SecondNum EndIf If RadioButton4.Checked = TrueThen----->But in here I have problems TextBox3.Text = FirstNum / SecondNum EndIf Catch ex As OverflowException MsgBox("Get real number out of range!", , "Entry error!") Calculater() ExitSub CatchWhenNot IsNumeric(TextBox1.Text) CatchWhenNot IsNumeric(TextBox2.Text) MsgBox("Enter a numeric value!", , "Entry error") Calculater() ExitSub EndTry End Sub __________________________________________________ ______________ Sub Calculater() TextBox1.Focus() TextBox1.SelectionStart = 0 TextBox2.SelectionStart = 0 TextBox1.SelectionLength = Len(TextBox1.Text) TextBox2.SelectionLength = Len(TextBox2.Text) EndSub __________________________________________________ ______________ I did not fine the answer to the zero devision I need to work just this part Addition,Substraction,multiplication works perfect I mean When I add 99999 + 99999 Shows me the warnig I put instead of the overflown messages and the programm do not stop the same with the Subtraction,multiplication I found the answer, but on division is so hard to get the same as all multiplication,Addition and Subtranction Please can any body help me Just division all my code I did I need help on the Division thanks all. |
|
#6
|
||||
|
||||
|
what's the problem? what error you get? you can manually check if SecondNum
is zero using If..Then and show the warning. |
|
#7
|
|||
|
|||
|
Not sure what is happening, but you appear to only be catching certain errors - what if the errors generated aren't what you are catching?
That is, where is your fail safe catch that will catch anything not already caught? |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Try...Catch help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|