Question: Language is C# and its in a windows application form You will add exception handling to your application created in part 3 Lab 4 the

Language is C# and its in a windows application form

You will add exception handling to your application created in part 3 Lab 4 the calculator. Exception handling helps you manage the state of the application after a try catch fails. Add a try-catch statement in 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. Add three additional catch blocks to the try-catch statement that will catch a FormatException, an OverflowException, and a DivideByZeroException. These catch blocks should display a dialog box with an appropriate error message.

Output should look like:

Language is C# and its in a windows application form You will

Existing Code:

namespace frmTryCatch { public partial class Form1 : Form { public Form1() { InitializeComponent(); }

private void btnCalculate_Click_1(object sender, EventArgs e) { double x, y; string p; /* console input data at text boxes */ x = Convert.ToDouble(txtOp1.Text); y = Convert.ToDouble(txtOp2.Text); p = txtOperator.Text; /* call method with three arguments operand 1 (x), operand 2 (y), and operator (p). */ calculate(x, y, p);

}

/* define function */ void calculate(double a, double b, string op) { /* print result at label upto 4 decimal space for different operators */ if (op == "+") { txtResult.Text = Math.Round((a + b), 4).ToString(); } else if (op == "-") { txtResult.Text = Math.Round((a - b), 4).ToString(); } else if (op == "*") { txtResult.Text = Math.Round((a * b), 4).ToString(); } else if (op == "/") { txtResult.Text = Math.Round((a / b), 4).ToString(); } }

//closes the application private void btnExit_Click(object sender, EventArgs e) { this.Close(); }

//resets the form txt fields private void btnReset_Click(object sender, EventArgs e) { txtOp1.Text = ""; txtOp2.Text = ""; txtOperator.Text = ""; txtResult.Text = ""; }

}

}

Simple Calculator 256 Operand 1 Operator: / Operand 2 ol Result: Entry Error Calculate Excit Divide-by-zero error. Please enter a non-zero value for operand 2. OK

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!