Throw DividebyZero exception RRS feed

  • Question

  • Quick simple question, I'm having trouble throwing a Divide by Zero exception dealing with a fraction class I created. Below is what I have in my fraction class, and I'd like to be able to call it to my button click event if the user enters a 0 into the denominator textbox. I've also included the button click event code for what I currently have

            public int Denominator
                    return Denominator;
                    if (value == 0)
                        throw new DividebyZeroException("Denominator", value, "Denominator cannot be zero");
            private void btnCalculateFraction_Click(object sender, EventArgs e)
                Fraction fr2 = new FractionApplication.Fraction(Convert.ToInt32(txtNumerator.Text), Convert.ToInt32(txtDenominator.Text));
                txtReduced.Text = "The lowest form of the inputted fraction is: " + fr2.ToString();

    I appreciate any feedback,

    Thank you.

    Friday, April 13, 2018 8:55 PM

All replies

  • What trouble are you having?

    Your code example isn't setting the Denominator property.  It's setting the values through the constructor.  Does the constructor set the value of the properties?

    Also, you aren't catching the exception, so that "throw" is going to cause the app to terminate.  Is that what you want?

    Tim Roberts, Driver MVP Providenza & Boekelheide, Inc.

    Friday, April 13, 2018 9:04 PM
  • The constructors do set the value of the properties I have 2 which are as follows:

            public Fraction()
                numerator = 0;
                denominator = 1;
            public Fraction(int num, int den)
                numerator = num;
                denominator = den;

    I'm trying to have it throw the exception if a user enters a 0 into the denominator textbox, I had to rewrite the code from a much longer version and after rewriting said code, it stopped throwing the exception and I can't seem to find what part of the code I changed in order to do so.  In short, yes, I want the app to terminate if the aforementioned box is a zero. (I didn't have it catch the exception with a try catch block in my previous code hence my conundrum here.)

    Friday, April 13, 2018 9:15 PM
  • Managed to figure it out, but now my program broke and is always displaying the denominator as 0 in the textbox, the fix was changing the numerator and denominator in the 2 parameter to uppercase N and D...now I have a whole new issue :(
    Friday, April 13, 2018 11:16 PM
  • Hello,

     I am sure, as we all been there at sometime or another, that broken

    projects can be a pain. But, you will have greater response and success

    when complete code is provided, omit IP code when warranted. 


     Posting code snippets that do not show code related to the posted issue

    is a waste of your time and then our time is spent on 20 question hoop



     Please provide the correct amount of information that allows anyone

    willing to use their free time in the most productive manner possible.


     Thanks :)

    Saturday, April 14, 2018 2:42 AM