Results 1 to 13 of 13

Thread: [RESOLVED] divide by 0 exception

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Resolved [RESOLVED] divide by 0 exception

    Hello guys, don't know why this code does not work. It does not compile at all

    Code:
    using System;
    
    class division
    {
        int i;
        int j;
    
        public division (int i, int j)
        {
           this.i = i;
            this.j = j;
        }
    
        public void divide(int n)
        {
            try
            {
                if (n/0) throw new DivideByZeroException("Divide by 0");
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Exception: {0}", e);
                return;
            }
            Console.WriteLine(i / j);
        }
    
    }
    
    class Test
    {
        public static void Main()
        {
            division d = new division(3, 1); 
            d.divide (2);
            Console.ReadLine ();
        }
    }

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: divide by 0 exception

    Well it won't compile because you're explicitly dividing by zero, which is not possible, so why would the compiler even let you try. I assume that you meant to test whether n was equal to zero.

    Apart from that, what purpose does the n argument serve? You're dividing i by j so it's j you should be testing for equality to zero, not n. n doesn't get used at all in the actual calculation.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    Yup, you are right.. i Just figured that out.. now I did little bit change, still my Main method does not even work:

    Code:
    using System;
    
    class division
    {
        int i;
        int j;
    
        public division (int i, int j)
        {
           this.i = i;
            this.j = j;
        }
    
        public void divide(int m, int n)
        {
            try
            {
                if (n==0) throw new DivideByZeroException("Divide by 0");
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Exception: {0}", e);
                return;
            }
            Console.WriteLine(m /n);
        }
    
    }
    
    class Test
    {
        public static void Main()
        {
            division d = new division(3, 1);
            Console.WriteLine("Please input two numbers: ");
            Console.WriteLine("{0}, {1}", Console.ReadLine(), Console.ReadLine());
            d.divide (Console.ReadLine (), Console.ReadLine());
            Console.ReadLine ();
        }
    }

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    This code is a messy one... if it could be clearly modified, I really appreciate, guys!

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    This is the updated codes, still not working:

    Code:
    using System;
    
    class division
    {
        int i;
        int j;
    
        public division (int i, int j)
        {
           this.i = i;
           this.j = j;
        }
    
        public void divide()
        {
            try
            {
                if (j==0) throw new DivideByZeroException("Divide by 0");
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine("Exception: {0}", e);
                return;
            }
            Console.WriteLine(i /j);
        }
    
    }
    
    class Test
    {
        public static void Main()
        {
            division d = new division(3, 0);
            Console.WriteLine("Please input two numbers: ");
            Console.WriteLine("{0}, {1}", Console.ReadLine(), Console.ReadLine());
            d.divide (Console.ReadLine (), Console.ReadLine());
           // d.divide();
            Console.ReadLine ();
        }
    }

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: divide by 0 exception

    What errors are you getting and why are you doing the division outside of the Try and Catch blocks?
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    Good question. I don't know! Does that division have to be in the try block? That's why I said it is a messy code. Also, I got the error that cannot convert string to Integer

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: divide by 0 exception

    Your 'divide' method is expecting two int objects. Console.ReadLine returns a string so you would have to read the two values, make sure that they are numbers, convert them and then call 'divide'. Also, you're calling ReadLine four times. That means that when the user enters two numbers the first two calls will be used to write the values back to the console, then it will wait for you to enter two more values. You should read from the console twice intot string variables. You can then do whatever you want with those values, like write them to the console or convert them to integers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    why instead of telling me this... cannot you just write the codes for me? So that I could pick it up faster than using the same mind that "created the problem"?

  10. #10
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: divide by 0 exception

    Quote Originally Posted by vbbit
    why instead of telling me this... cannot you just write the codes for me? So that I could pick it up faster than using the same mind that "created the problem"?
    If we did everything for you, you wouldn't learn. Besides, this could be a homework assignment and doing so could get you expelled.

    Also, there are tons of examples online about receiving console input and making simple calculators and such.
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: divide by 0 exception

    Because it's better if the same mind that created the problem can solve the problem. Writing the code yourself is a better way to understand it than copying and pasting something someone else wrote. Making mistakes is part of the learning process, and so is fixing them.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,170

    Re: divide by 0 exception

    Thanks guys.. I understand now... I got it worked. YAY!

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: divide by 0 exception

    More satisfying when you do it yourself, isn't it? Don't forget to resolve your thread from the Thread Tools menu.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width