Solved

send a message if try to divide by zero

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 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

19 Experts available now in Live!

Get 1:1 Help Now