Link to home
Start Free TrialLog in
Avatar of magenta
magenta

asked on

How to catch an Integer Overflow Exception?

I am trying to catch an integer overflow becuase my program adds up a bunch of large numbers with a potential for overflow.  I read through the SEH docs/examples and I wrote up a simple test program as follows:

  #include <windows.h>
  #include <fstream.h>

  int main()
  {
    unsigned long value = 4294967293;
    for( int i = 0; i < 5; i++ )
    {
      __try
      {
        value = value + 1;
      }
      __except( ( GetExceptionCode() ==
                  EXCEPTION_INT_OVERFLOW ) ?                         EXCEPTION_EXECUTE_HANDLER :
                EXCEPTION_CONTINUE_SEARCH )
      {
        cout << "overflow!" << endl;
      }
      cout << value << endl;
    }
    return( 0 );
  }

The problem is, I cannot catch the overflow of the variable "value"!  The output is as follows:

  4294967294
  4294967295
  0
  1
  2

If I changed the code to divide by zero somehow, and change
EXCEPTION_INT_OVERFLOW to EXCEPTION_INT_DIVIDE_BY_ZERO above, then the divide by zero exception is caught.  

Even if I switch to C++ exception handling (by replacing "__try" with "try" and "__except" with "catch( ... )") I still can't catch anything.  Any ideas what I am doing wrong?

EXCEPTION_INT_OVERFLOW is defined as "The result of an integer operation caused a carry out of the most significant bit of the result."  But that's all the documentation I can find on it.

Thanks!

Later,
Frank
Avatar of chensu
chensu
Flag of Canada image

Try changing the type of value from unsigned to signed to see what happens.
Avatar of magenta
magenta

ASKER

OK, I changed "unsigned long value = 4294967293;" to "long value = 2147483645;" but it still doesn't work!  The output is:

  2147483646
  2147483647
  -2147483648
  -2147483647
  -2147483646

I forgot to mention that the above should compile with VC++ 5.  Just create a Win32 Console app and create a C++ source file.

Any ideas?  Thanks...
ASKER CERTIFIED SOLUTION
Avatar of faster
faster

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Correction.  No integer overflow exception at all.  Not even for the multiplication and division.  
Yes, I agree with faster and nietod. If you take out the try-except statement, the program is running well. It won't terminate like access violation or divide-by-zero exception. But, what is EXCEPTION_INT_OVERFLOW for?
Hi niotod, don't forget I am "faster", want to change your name to "fastest"?
I can't be fastest as long as you are faster.

Chensu, this stuff seems to be kinda hardware dependant.  The x86 the processor just sets an overflow flag when an overflow occurs and does not interrupt.  However, some other hardware (other processors) might catch integer overflows.  Probably not on addition or subtraction, but on other operations, like multiply or divide.  The flag is probably for use on that hardware, if or when it is available.  (Something I used once did interrupt on a multiply/divide overflow, but it not the x86.  Perhaps it was a 68000.)
It seems that the answer to my question is that the Intel Processor does not support overflow exceptions (I'm running on a Pentium Pro 200, but I haven't tried any other CPUs).  I think that MS should add a note to their documentation if certain excpetions are not available on all processors---at least a warning would be nice.