0

I have a task to make a calculation of selling price and cost value. I don't know have any idea to make a good coding for exception.

Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
    Dim SellingPrice As Double
    Dim CostValue As Double
    Dim Commission As Double
    Dim CommissionRate As Double = 0.2

    If Me.TextBoxSellingPrice.Text <> "" Or Me.TextBoxCostValue.Text <> "" Then

        Try
            SellingPrice = Double.Parse(TextBoxSellingPrice.Text)
            CostValue = Double.Parse(TextBoxCostValue.Text)

            'Calculation
            Commission = CommissionRate * (SellingPrice - CostValue)

            'Display
            Me.TextBoxDisplay.Text = Me.TextBoxName.Text
            Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")
        Catch
            MessageBox.Show("Wrong Input", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    Else
        MessageBox.Show("Please fill your data completely", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End If

End Sub

Output: Figure 1

cyboashu
  • 10,196
  • 2
  • 27
  • 46
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
  • By `coding for exceptional` do you mean the Exception handling? If you use `Double.TryParse()` you really dont need an exception handler – Ňɏssa Pøngjǣrdenlarp Feb 13 '18 at 01:19
  • As @Plutonix suggests, you should generally not try to catch exceptions that you can avoid in the first place. Validate user input before using it and then an exception can't be thrown. `TryParse` method do the validation and conversion in one step. – jmcilhinney Feb 13 '18 at 01:20
  • I mean the `Else MessageBox.Show("Please fill your data completely", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information) ` it is ok using if else concept for this exception handling? – Ticherhaz FreePalestine Feb 13 '18 at 01:22
  • That outer check is not exception handling - it is the data validation @jmcilhinney mentioned. You could also just `Return` if it fails so you dont have an else or an exception handler if you use `TryParse` - much simpler all way around – Ňɏssa Pøngjǣrdenlarp Feb 13 '18 at 01:25
  • 1
    There's really no point here differentiating between a blank field and a field containing "Hello World". Neither can be converted to a number so both are wrong. Calling `TryParse` will convert the text to a number if it's valid and tell you without an exception if it's not. You have two experienced developers telling you to use `TryParse` here. You should probably look into it. – jmcilhinney Feb 13 '18 at 02:00
  • I will take a note of that @jmcilhinney – Ticherhaz FreePalestine Feb 13 '18 at 16:13

1 Answers1

2

Using exception handling to check that data is correct isn't really the best practice. For a start, the performance of Try..Catch blocks is pretty poor compared to just using If..Then..Endif as there is a fair amount of stuff happening in the background with exceptions. In your case it wont make a major difference, but if the Try..Catch..End Try is in a loop, your program can take quite a performance hit.

There is little else to do other than to write some If..End If statements to validate your input data .. As other commenters had stated, using .TryParse is an exellent way to start things off ..

If Not Double.TryParse(TextBoxSellingPrice.Text, SellingPrice) Or Not Double.TryParse(TextBoxCostValue.Text, CostValue) Then
    MessageBox.Show("Invalid input. Please enter positive numbers only")
    Exit Sub
End If
If SellingPrice < CostValue Then
    MessageBox.Show("Selling Price must be greater than cost")
    Exit Sub
End If
If Not SellingPrice > 0 Or Not CostValue > 0 Then
    MessageBox.Show("Selling price and cost must be >0")
    Exit Sub
End If
'Calculation
Commission = CommissionRate * (SellingPrice - CostValue)
'Display
Me.TextBoxDisplay.Text = Me.TextBoxName.Text
Me.TextBoxCommission.Text = "RM" + Commission.ToString("F2")

You could also have a look at the ErrorProvider component

David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • Nit: There is very little to no cost to having a `Try/Catch` block. The cost comes when there is one and/or playing with the StackTrace.. There are several MSIL type posts here and other [Summary answers](https://stackoverflow.com/q/2075151). That said, using them for program flow when there are more proactive ways to test such as `TryParse` – Ňɏssa Pøngjǣrdenlarp Feb 13 '18 at 15:13
  • I got the info, it really helping me. thank you so much! – Ticherhaz FreePalestine Feb 13 '18 at 16:13