Solved

send a message if try to divide by zero

Posted on 2009-06-29
5
198 Views
Last Modified: 2013-12-17
Hello, I have a windows form 2 textboxes, 1, button, 1 label.
I click button and first number is divided by second.
the result is displayed in a label.
I have an exception that if someone inputs a letter an error will come back- but I also want a message to show if I try to divide by 0.
I have pasted code.
thank you
private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double num1 = double.Parse(textBox1.Text);
                double num2 = double.Parse(textBox2.Text);
 
                label1.Text = (num1 / num2).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("error:" + ex.Message);
            }
        }

Open in new window

0
Comment
Question by:davetough
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:eirikurh
ID: 24741482
           try
            {
                double num1 = double.Parse(textBox1.Text);
                double num2 = double.Parse(textBox2.Text);
 
                label1.Text = (num1 / num2).ToString();
            }
            catch (DivideByZeroException ex)
            {
                MessageBox.Show("division by zero error:" + ex.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show("error:" + ex.Message);
            }


But i would always try to avoid exceptions
            double num1 = double.Parse(textBox1.Text);
            double num2 = double.Parse(textBox2.Text);
           if (num1 > 0 && num2 > 2)
           {
            label1.Text = "0"
.....

0
 

Author Comment

by:davetough
ID: 24741536
your most likely right about avoiding - but I am trying to see how it works:
I may be doing something wrong but- I pasted code in and If I divide by zero I get infinity in label
thank you
0
 
LVL 3

Expert Comment

by:eirikurh
ID: 24741717
http://msdn.microsoft.com/en-us/library/system.dividebyzeroexception.aspx
The exception that is thrown when there is an attempt to divide an integral or decimal value by zero.

Doesn´t work for double values, they always +- Infinity

try using decimal instead

                decimal num1 = Int32.Parse(textBox1.Text);
                decimal num2 = Int32.Parse(textBox2.Text);
               
                label1.Text = (num1 / num2).ToString();

0
 
LVL 3

Accepted Solution

by:
eirikurh earned 500 total points
ID: 24741721
decimal num1 = Decimal.Parse(textBox1.Text);
decimal num2 = Decimal.Parse(textBox2.Text);
               
label1.Text = (num1 / num2).ToString();
0
 

Author Comment

by:davetough
ID: 24741865
thank you
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question