Try...Catch help

vargf

Member
Joined
Feb 15, 2006
Messages
13
Programming Experience
Beginner
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:
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 anybody Help me?
:confused:
 
Try ' I try this to see if works but don't work
If RadioButton4.Checked = True Then
TextBox3.Text = FirstNum / SecondNum
End If
Catch ex as exception
messagebox.show(ex.message)
End Try

Don't bother with any of that err.raise stuff, it's part the 'old' style vb.
 
if you want to raise an error to your upper-level application layer, you can use the Throw command. for example:
Try
dim iNum as integer = 1/0 'will throw an overrun exception
catch ex as exception
throw ex
end try

or if you just want to throw an exception at any point in the code, you can simply create a new exception object and throw it... for example:
Throw(New Exception("You can not divide by zero."))
 
In my opinion not a good way to use structured exception handling. Having looked again this way would probably be best......

You can write a generic exception handling routine than can handle the different types of exception that your app is likely to throw. i.e

VB.NET:
catch Ex as system.dividebyzeroexception
'Add code to handle this error
 
Catch Ex as system.overflowexception
'Add code to handle this exception.
then at the bottom catch any other exception that you haven't expected..

VB.NET:
Catch ex as exception
'Further error handling
 
If at all possible AVOID throwing exceptions. They are expensive and will slow things down considerably.

Also, it's best to validate the data BEFORE attempting calculations. If a divide by zero error is possible, check the data FIRST before doing the division, and then notify the user if necessary. Do NOT, I repeat DO NOT use exception handling as a means of data validation. Do everything possible to prevent the error in the first place.

-tg
 
Good post TechGnome. Just out of curiosity, what is trowing an excetpion? I understand the concept of:

VB.NET:
Try
  ' try code here
Catch ex as exception
  ' handling here
End try
 
But what is the Throw used for?
 
That's catching an error there.... throwing is programatically creating an error to force the code into the catch.... but even simply having an exception happen anyways is expesive...the best thing to do is to prevent the error from happening in the first place. THen you only should have try catches around the most critical calls in your system - like in making database calls or sending data out to a web service.

-tg
 
Here are my thoughts: Exceptions are expensive, no doubt. But, this is the way the .NET runtime handles errors. I agree with trying to avoid them. But you can make a mess for yourself if you try and mix too many different error handling schemes in your design. If it really is an unrecoverable error than use exceptions. If you do, you can build consistent logging and handling of exceptions in a centralized manner instead of having thousands of "if HRESULT <> 0" checks in your code.
 
Back
Top