Solved

send a message if try to divide by zero

Posted on 2009-06-29
5
196 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now