[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

send a message if try to divide by zero

Posted on 2009-06-29
5
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

656 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