Question: I have asked this quesiton before and I get code for C# but I am working with Visual Basic. Does anyone know how to code

I have asked this quesiton before and I get code for C# but I am working with Visual Basic. Does anyone know how to code this for Visial Basic?

Exercise 7-1 Add exception handling to the simple calculator

In this exercise, youll add exception handling to the Simple Calculator form of exercise 6-1.

1. Open the SimpleCalculator project in the Assignment Exercises\Chapter 07\SimpleCalculator With Exception Handling directory.

2. Add a TryCatch statement to the btnCalculate_Click event handler that will catch any exceptions that occur when the statements in that event handler are executed. If an exception occurs, display a dialog box with the error message, the type of error, and a stack trace. Test the application by entering a nonnumeric value for one of the operands.

3. Add three additional catch blocks to the TryCatch statement that will catch an InvalidCastException, an OverflowException, and a DivideByZeroException. (If you used the ToDecimal method of the Convert class instead of the CDec function to convert the operands entered by the user to decimals, you should catch a FormatException instead of an InvalidCastException.) These catch blocks should display a dialog box with an appropriate error message.

4. Test the application again by entering a nonnumeric value for one of the operands. Then, enter 0 for the second operand as shown above to see what happens.

Here is my current Visual Basic code:

Public Class Form1

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click Dim operand1 As Decimal = CDec(txtOperand1.Text) Dim operator1 As String = txtOperator.Text Dim operand2 As Decimal = CDec(txtOperand2.Text) Dim result As Decimal = CalculationResult(operand1, operator1, operand2)

result = Math.Round(result, 4) txtResult.Text = result.ToString End Sub

Private Function CalculationResult(operand1 As Decimal, operator1 As String, operand2 As Decimal) As Decimal If operator1 = "+" Then CalculationResult = operand1 + operand2 ElseIf operator1 = "-" Then CalculationResult = operand1 - operand2 ElseIf operator1 = "*" Then CalculationResult = operand1 * operand2 ElseIf operator1 = "/" Then CalculationResult = operand1 / operand2 End If Return CalculationResult End Function

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click Me.Close() End Sub

Private Sub ClearResult(sender As Object, e As EventArgs) _ Handles txtOperand1.TextChanged, txtOperator.TextChanged, txtOperand2.TextChanged txtResult.Text = "" End Sub

End Class

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!