none
divide by zero

    Question

  • Hello everyone,

     


    I have verified that the result of 100/0 will not occur any exception, and it will be infinite.

     

    Does it mean there is no exceptions like divide by zero in C#?

     


    thanks in advance,
    George

    Thursday, April 24, 2008 9:30 AM

Answers

  •  George2 wrote:

    Hello everyone,

     


    I have verified that the result of 100/0 will not occur any exception, and it will be infinite.

     

    Does it mean there is no exceptions like divide by zero in C#?

     


    thanks in advance,
    George



    You have verified wrong then.

    This code throws a divide by zero exception:

                int x = 1;
                int y = 0;
                int z = x/y;

    What code did you use to test it?

    Here's the MS documentation for the exception that gets thrown for divide by zero:

    http://msdn2.microsoft.com/en-us/library/system.dividebyzeroexception.aspx
    Thursday, April 24, 2008 9:34 AM

All replies

  •  George2 wrote:

    Hello everyone,

     


    I have verified that the result of 100/0 will not occur any exception, and it will be infinite.

     

    Does it mean there is no exceptions like divide by zero in C#?

     


    thanks in advance,
    George



    You have verified wrong then.

    This code throws a divide by zero exception:

                int x = 1;
                int y = 0;
                int z = x/y;

    What code did you use to test it?

    Here's the MS documentation for the exception that gets thrown for divide by zero:

    http://msdn2.microsoft.com/en-us/library/system.dividebyzeroexception.aspx
    Thursday, April 24, 2008 9:34 AM
  • Hi Matthew,

     

     

    Here is my code. Any comments? No exception caught. :-)


    Code Snippet

            static void Main(string[] args)
            {
                try
                {
                    float a = 100;
                    float b = 0;
                    float c = a / b;
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0}", ex.ToString());
                }
               

                return;
            }
        }

     

     

     

     Matthew Watson wrote:

     George2 wrote:

    Hello everyone,

     


    I have verified that the result of 100/0 will not occur any exception, and it will be infinite.

     

    Does it mean there is no exceptions like divide by zero in C#?

     


    thanks in advance,
    George



    You have verified wrong then.

    This code throws a divide by zero exception:

                int x = 1;
                int y = 0;
                int z = x/y;

    What code did you use to test it?

    Here's the MS documentation for the exception that gets thrown for divide by zero:

    http://msdn2.microsoft.com/en-us/library/system.dividebyzeroexception.aspx

     

     

    regards,

    George

    Thursday, April 24, 2008 9:56 AM
  • Divide by zero is only thrown for integer types.

    Read the link I posted in my first reply - it tells you all about it!

    Note that if you say "100/0" then you are talking about integers, since those numbers don't have decimal points.
    Thursday, April 24, 2008 10:12 AM
  • Like Matthew said, read his link

    It's also stated in other places:
    Floating point specification
    C# operators even mentions it

    somewhere out there there was a good table of the results of floating point division, (the best table I've found so far is here, if you find the PDF of the C# language specs, it has the chart in a more readable format)

    You should check it using the floating point operators of float.IsInfinity() and float.IsNaN() for those respective error conditions, 100/0 should cause the value of float.PositiveInfinity, it's a specific floating point valuedefined by the IEEE specification.
    (-100/0 should result in float.NegativeInfinity). Also, technically the floating point zero can be signed (0 and -0 are different)
    Thursday, April 24, 2008 4:51 PM
  • Thanks Matthew and IsshouFuuraibou,

     

     

    In what kinds of operations, will NaN be produced, in what kinds of operations, will infinity be produced -- I am always confused about them two.

     

    Any documents for them?

     

     IsshouFuuraibou wrote:
    Like Matthew said, read his link

    It's also stated in other places:
    Floating point specification
    C# operators even mentions it

    somewhere out there there was a good table of the results of floating point division, (the best table I've found so far is here, if you find the PDF of the C# language specs, it has the chart in a more readable format)

    You should check it using the floating point operators of float.IsInfinity() and float.IsNaN() for those respective error conditions, 100/0 should cause the value of float.PositiveInfinity, it's a specific floating point valuedefined by the IEEE specification.
    (-100/0 should result in float.NegativeInfinity). Also, technically the floating point zero can be signed (0 and -0 are different)

     

     

    regards,

    George

    Friday, April 25, 2008 1:12 PM
  • Well there are the ECMA specs

    I did find a good chart out there in a HTML version of the specifications: Here (for division)
    Friday, April 25, 2008 3:34 PM
  • Thanks IsshouFuuraibou,

     

     

    Question answered.

     

     IsshouFuuraibou wrote:
    Well there are the ECMA specs

    I did find a good chart out there in a HTML version of the specifications: Here (for division)

     

     

    regards,

    George

    Saturday, April 26, 2008 2:06 PM
  • Thanks IsshouFuuraibou,

     

     

    Question answered.

     

     IsshouFuuraibou wrote:
    Well there are the ECMA specs

    I did find a good chart out there in a HTML version of the specifications: Here (for division)

     

     

    regards,

    George

    Saturday, April 26, 2008 2:20 PM
  • Thanks IsshouFuuraibou,

     

     

    Question answered.

     

     IsshouFuuraibou wrote:
    Well there are the ECMA specs

    I did find a good chart out there in a HTML version of the specifications: Here (for division)

     

     

    regards,

    George

    Saturday, April 26, 2008 2:21 PM
  • Thanks IsshouFuuraibou,

     

     

    Question answered.

     

     IsshouFuuraibou wrote:
    Well there are the ECMA specs

    I did find a good chart out there in a HTML version of the specifications: Here (for division)

     

     

    regards,

    George

    Saturday, April 26, 2008 2:25 PM
  • I am working with asp.net webpage and have experiences the following: 

    The code below throws and suppresses the exception as expected

           try
            {
                throw new ApplicationException("error");
            }
            catch (Exception ex)
            {
            }

    ---------

    The code below throws the exception but does not suppress.  Can anyone explain why the exception is not being suppressed but instead propagated?

            try
            {
                int i = 2 / 0;
            }
            catch (Exception ex)
            {
            }

    software developer jobs

    Thursday, February 09, 2012 10:08 PM