simple calculator windows .net application and use of try catch

This is a next video after hello world application and in this video we will see how to use basic math operators like addition, subtraction, multiplication etc.
Also here we learn use of “try and catch” module to handle exception occurring during division operation when we will try to divide any number by 0.

 

Coding Sample

private void btnPlus_Click(object sender, EventArgs e)
{
try {
decimal result = Convert.ToDecimal(txtNumber1.Text) + Convert.ToDecimal(txtNumber2.Text);
txtResult.Text = result.ToString();
}
catch { MessageBox.Show(“Invalid input!!!”); }
}

private void btnminus_Click(object sender, EventArgs e)
{
try
{
decimal result = Convert.ToDecimal(txtNumber1.Text) – Convert.ToDecimal(txtNumber2.Text);
txtResult.Text = result.ToString();
}
catch {
MessageBox.Show(“Invalid input!!!”);
}
}

private void btnMulty_Click(object sender, EventArgs e)
{
try
{
decimal result = Convert.ToDecimal(txtNumber1.Text) * Convert.ToDecimal(txtNumber2.Text);
txtResult.Text = result.ToString();
}
catch { MessageBox.Show(“Invalid input!!!”); }
}

private void btnDivision_Click(object sender, EventArgs e)
{
try
{
decimal result = Convert.ToDecimal(txtNumber1.Text) / Convert.ToDecimal(txtNumber2.Text);
txtResult.Text = result.ToString();
}
catch { MessageBox.Show(“Invalid input!!!”); }
}

2 Replies to “simple calculator windows .net application and use of try catch”

Leave a Reply

Your email address will not be published. Required fields are marked *