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!!!”); }
}

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Leave a Reply

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