divide by zero error ignored !

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aniait
    New Member
    • Jul 2008
    • 4

    divide by zero error ignored !

    Hi,

    I just came across a faulty code of mine when working on Symbian C++ code. It threw an exception for the code fragment below (or something similar to this):

    Code:
    int x;
    for(int i = 2; i >= 0 ; i--)
    	{
    	x = 5 % i; // [B]Divide by zero exception here when i = 0.[/B]
    	x++;
    	}
    So, I tried the same on Visual C++ and gcc compiler. The funny thing is, I do not get an exception when I run the code. An exception is thrown only in debug mode.

    Can someone tell me what is happening here?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Well when I built using VS 2005 I get an exception in when run in the debugger and when run normally (although they are handled slightly differently).

    I believe divide by 0 is undefined behaviour so anything could happen.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Banfa
      I believe divide by 0 is undefined behaviour so anything could happen.
      True; this is what ANSI C99 has to say about it:

      Originally posted by C99
      [#5] The result of the / operator is the quotient from the
      division of the first operand by the second; the result of
      the % operator is the remainder. In both operations, if the
      value of the second operand is zero, the behavior is
      undefined.
      So there is no defined need to throw an exception when a division by zero
      occurs although most hardware has support for it.

      kind regards,

      Jos

      Comment

      Working...