Solved

send a message if try to divide by zero

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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

687 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