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
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?
When you CATCH you will want to add; return
Without the "return" keyword the code continues
For one, get rid of the
that you have 3 times. You just need
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.
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.
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.
I usually use
I don't know what the difference would be between Return or just to Exit Sub.