Try Catch with a "Try" for 3 User Text inputs

My code is at the very bottom and it works. My teacher wants a "Try" for each of the three user inputs. I cannot get it to work that way. He wrote it on the board for us to do like the below. That doesn't work at all for me. How can I have multiple, or nested "Try" blocks for each user input as he wants?

 Try "input"
 
    Try "input"
  
       Try "input"
       
            All calculations
            
            Catch As Format Exception
 End Try



 Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim begInv As Double
    Dim endInv As Double
    Dim avgInv As Double
    Dim turnover As Double
    Dim costSold As Double
    Try
        begInv = Double.Parse(txtBegInv.Text)


        endInv = Double.Parse(txtEndInv.Text)


        costSold = Decimal.Parse(txtCostSold.Text)
        avgInv = (begInv + endInv) / 2
        turnover = Math.Round(costSold / avgInv, 1)
        txtAvgInv.Text = avgInv.ToString("C")
        txtTurnover.Text = turnover.ToString

    Catch ex As FormatException
        MessageBox.Show("Please check your values.")

    End Try

Posted Oct 11, 2017, 12:17 AM UTC · edited

28
type0nullOP•
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    Dim begInv As Double
    Dim endInv As Double
    Dim avgInv As Double
    Dim turnover As Double
    Dim costSold As Double
    Try
        begInv = Double.Parse(txtBegInv.Text)
    Catch ex As FormatException
        MessageBox.Show("Please check your values.", "Beginning Inventory Error")
        With txtBegInv.Focus
        End With
    End Try
    Try
        endInv = Double.Parse(txtEndInv.Text)
    Catch ex As FormatException
        MessageBox.Show("Please check your values.", "Ending Inventory Error")
        With txtEndInv.Focus
        End With
    End Try
    Try
        costSold = Decimal.Parse(txtCostSold.Text)
    Catch ex As FormatException
        MessageBox.Show("Please check your values.", "Cost of Goods Sold Error")
        With txtCostSold.Focus
        End With
    End Try
    avgInv = (begInv + endInv) / 2
    turnover = Math.Round(costSold / avgInv, 1)
    txtAvgInv.Text = avgInv.ToString("C")
    txtTurnover.Text = turnover.ToString



End Sub

     End Class

After some trial and error that is what I came up with. My only problem now is even when an error occurs, the formulas still execute and give me an output. How would I avoid that?

1
JamesWjRose•

When you CATCH you will want to add; return

Without the "return" keyword the code continues

1
TheFotty•

For one, get rid of the

With textboxname.focus
end with

that you have 3 times. You just need

textboxname.focus

There is no need for a with block when calling a single method on an object, and its not being used correctly in your syntax anyway.

As to your question, if an exception is hit, you should have an "exit sub" in the catch block so that it will exit the subroutine when an error is hit and not process the last 4 lines of code which are your calculations.

1
type0nullOP•

Ok thank you. My response to your With/End suggestion is that he wants all the text to be highlighted using the With/End technique. Wrong or right it's what he wants. Is there a way to have all text highlighted when the text box gets focus.

1
TheFotty•
with textboxnamehere
    .focus
    .SelectAll
end with
1
type0nullOP•

Any particular reasons why the .SelectAll option is not appearing or working for me for either method?

I like what you suggested, also. Why do the With/End With when it's not actually necessary? Thank you.

1
marcmiller2007•

I usually use

Exit Sub

I don't know what the difference would be between Return or just to Exit Sub.

1