Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advanced MySQL 8 Discover The Full Potential Of MySQL And Ensure High Performance Of Your Database

Authors: Eric Vanier ,Birju Shah ,Tejaswi Malepati

1st Edition

1788834445, 978-1788834445

More Books

Students also viewed these Databases questions

Question

why we face Listening Challenges?

Answered: 1 week ago

Question

what is Listening in Context?

Answered: 1 week ago