Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old March 8th, 2006, 11:12 PM
zorro23 zorro23 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 zorro23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 45 sec
Reputation Power: 0
Question Try...Catch help

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

Reply With Quote
  #2  
Old March 10th, 2006, 01:30 AM
nofriends's Avatar
nofriends nofriends is offline
Senior Water Wizard
ASP Free God 11th Plane (10000 - 10499 posts)
 
Join Date: Aug 2004
Location: Cape Town, RSA
Posts: 10,186 nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)nofriends User rank is Brigadier General (60000 - 70000 Reputation Level)  Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1Folding Points: 106940 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 2 Weeks 2 Days 7 h 36 m 24 sec
Reputation Power: 699
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



Reply With Quote
  #3  
Old March 10th, 2006, 04:46 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 46th Plane (27500 - 27999 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,641 Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 2 Weeks 3 h 14 m 36 sec
Reputation Power: 1902
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.

Reply With Quote
  #4  
Old March 10th, 2006, 06:41 PM
LozWare's Avatar
LozWare LozWare is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jun 2005
Posts: 531 LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level)LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level)LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level)LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level)LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level)LozWare User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 10 h 56 m 31 sec
Reputation Power: 46
Send a message via MSN to LozWare
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.

Reply With Quote
  #5  
Old March 11th, 2006, 01:51 PM
zorro23 zorro23 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 3 zorro23 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 45 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old March 11th, 2006, 05:03 PM
Shadow Wizard's Avatar
Shadow Wizard Shadow Wizard is offline
Moderator From Beyond
ASP Free God 46th Plane (27500 - 27999 posts)
 
Join Date: Sep 2004
Location: Israel
Posts: 27,641 Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)Shadow Wizard User rank is General 14th Grade (Above 100000 Reputation Level)  Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1Folding Points: 374417 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 3 Months 2 Weeks 3 h 14 m 36 sec
Reputation Power: 1902
what's the problem? what error you get? you can manually check if SecondNum
is zero using If..Then and show the warning.

Reply With Quote
  #7  
Old March 16th, 2006, 07:36 PM
q97 q97 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: Brisvegas
Posts: 276 q97 User rank is Sergeant Major (2000 - 5000 Reputation Level)q97 User rank is Sergeant Major (2000 - 5000 Reputation Level)q97 User rank is Sergeant Major (2000 - 5000 Reputation Level)q97 User rank is Sergeant Major (2000 - 5000 Reputation Level)q97 User rank is Sergeant Major (2000 - 5000 Reputation Level)q97 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 4 Days 10 h 41 m 49 sec
Reputation Power: 37
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?

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Try...Catch help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT