I am beginning to use exceptions in visual basic and haveing issues with my compute button(totals button works fine). My issue is I have a group box of customer information and the customer name, shipping address, and telephone cannot be blank and just need help on how I can throw exception with this. I also have a purchase information group box that checks to make sure product identifier, purchase price, and quantity purchased are not blank either. I was able to get this to work fine for purchase price and quantity purchased but does not work for product identifier. If someone could please walk me through this so I hopefully wont have to ask how to do exceptions again. Here is my code and if I need to explain anything better please let me know...

'Declare constant for sales tax
Const SALES_TAX As Single = 0.05
Const FED_EX_AIR As Single = 25.0
Const FED_EX_GROUND As Single = 20.0
Const US_POSTAL As Single = 12.0
Const ONE_YEAR_WARRANTY As Single = 75.0
Const CLUB_MEMBERSHIP As Single = 150.0
Const TIRE_SALE As Single = 25.0
Const WHOLESALE_DISCOUNT As Single = 0.1
Const RETAIL_DISCOUNT As Single = 0.0

'Determine the shipping cost if fed ex air is clicked
If fedExAirRadioButton.Enabled Then
shippingCostDecimal = FED_EX_AIR
ElseIf fedExGroundRadioButton.Enabled Then
shippingCostDecimal = FED_EX_GROUND
ElseIf USPostalRadioButton.Enabled Then
shippingCostDecimal = US_POSTAL
End If

'Show the results in a textbox
shippingCostTextBox.Text = shippingCostDecimal

'Check extras
If oneYearWarrantyCheckBox.Checked Then
oneYearWarrantyDecimal = ONE_YEAR_WARRANTY
End If
If clubMembershipCheckBox.Checked Then
clubMembershipDecimal = CLUB_MEMBERSHIP
End If
If tireSaleCheckBox.Checked Then
tireSaleDecimal = TIRE_SALE
End If

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
'Exit the program with being prompted to exit
Dim response As Integer
response = MessageBox.Show("Do you want to close the form?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

If response = vbYes Then
Me.Close()
End If
End Sub

End Class

November 19th, 2012, 01:39 PM

dglienna

Re: exception errors

You don't need to use EXCEPTION HANDLING for that. That is more for system errors, not program errors. Just check if the field(s) are BLANK or NOTHING, and set a flag for each one. Set it FALSE before the checks, and if there is ANY error, each one can set it to TRUE. Check for TRUE before executing the code which might cause the exception. Of course, if there is any chance of a missing CD or Network Share, then, use and EXCEPTION. ON ERROR XXX

Here's a quick sample:

Code:

Public Function DigestFileToHS(InFile As String) As String
On Error GoTo errorhandler
GoSub begin

'Declare constant for sales tax
Const SALES_TAX As Single = 0.05
Const FED_EX_AIR As Single = 25.0
Const FED_EX_GROUND As Single = 20.0
Const US_POSTAL As Single = 12.0
Const ONE_YEAR_WARRANTY As Single = 75.0
Const CLUB_MEMBERSHIP As Single = 150.0
Const TIRE_SALE As Single = 25.0
Const WHOLESALE_DISCOUNT As Single = 0.1
Const RETAIL_DISCOUNT As Single = 0.0

If telephoneMaskedTextBox.Text = String.Empty Then
MessageBox.Show("Phone number cannot be blank", "Telephone error",
MessageBoxButtons.OK, MessageBoxIcon.Error)
customerNameTextBox.Focus()
End If
'Determine the shipping cost if fed ex air is clicked
If fedExAirRadioButton.Enabled Then
shippingCostDecimal = FED_EX_AIR
ElseIf fedExGroundRadioButton.Enabled Then
shippingCostDecimal = FED_EX_GROUND
ElseIf USPostalRadioButton.Enabled Then
shippingCostDecimal = US_POSTAL
End If

'Show the results in a textbox
shippingCostTextBox.Text = shippingCostDecimal

'Check extras
If oneYearWarrantyCheckBox.Checked Then
oneYearWarrantyDecimal = ONE_YEAR_WARRANTY
End If
If clubMembershipCheckBox.Checked Then
clubMembershipDecimal = CLUB_MEMBERSHIP
End If
If tireSaleCheckBox.Checked Then
tireSaleDecimal = TIRE_SALE
End If

Private Sub exitButton_Click(sender As Object, e As EventArgs) Handles exitButton.Click
'Exit the program with being prompted to exit
Dim response As Integer
response = MessageBox.Show("Do you want to close the form?", "Quit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

It should help identify the error. You can use an ON ERROR statement, with the TRY/CATCH

November 20th, 2012, 12:05 PM

WizBang

Re: exception errors

Doesn't look quite like classic VB6 to me. is this .net you're working with by any chance?

November 20th, 2012, 12:31 PM

bkruep

Re: exception errors

I used your advice and added a messagebox to check each textbox and worked great with the exception of the masked textbox. I am guessing since there are '(', ')', and '-' already in the textbox that I will need to use a different way of checking to make sure all 10 digits are entered. All i found was the following code which did not work

If telephoneTextbox.Text = nothing then

Here is the code which I have shortened up....

Code:

'Declare Dim statements to check for errors
Dim customerNameError As New System.Text.StringBuilder
Dim shippingAddressNameError As New System.Text.StringBuilder
Dim telephoneError As New System.Text.StringBuilder
Dim productIdentifierError As New System.Text.StringBuilder

'If statement to check if customer name is blank
If String.IsNullOrWhiteSpace(customerNameTextBox.Text) Then
customerNameError.AppendLine("Name required")
customerNameTextBox.Focus()
End If

'Message box for error in customer name
If customerNameError.ToString.Length > 0 Then
MessageBox.Show(customerNameError.ToString, "Error in Customer Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

'If statement to check if shipping address is blank
If String.IsNullOrWhiteSpace(shippingAddressTextBox.Text) Then
shippingAddressNameError.AppendLine("Shipping address required")
shippingAddressTextBox.Focus()
End If

'Message box for error in shipping address
If shippingAddressNameError.ToString.Length > 0 Then
MessageBox.Show(shippingAddressNameError.ToString, "Error in Shipping Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

'If statement to check if telephone number is valid
If String.IsNullOrWhiteSpace(telephoneMaskedTextBox.Text) Then
telephoneError.AppendLine("Telephone required")
End If

'Message box for error in telephone number
If telephoneError.ToString.Length < 0 Then
MessageBox.Show(customerNameError.ToString, "Error in Telephone Number", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

'If statement to check if product identifier is blank
If String.IsNullOrWhiteSpace(productIdentifierTextBox.Text) Then
productIdentifierError.AppendLine("Product Identifier required")
productIdentifierTextBox.Focus()
End If

'Messagebox for error in product identifier
If productIdentifierError.ToString.Length < 0 Then
MessageBox.Show(productIdentifierError.ToString, "Errors in Purchase Information", MessageBoxButtons.OK, MessageBoxIcon.Error)
Exit Sub
End If

November 20th, 2012, 01:09 PM

dglienna

Re: exception errors

Use a HIDDEN textbox for the MASKED TB. Let user type into normal textbox, check it, and then show the MTB with the value