How to catch an Integer Overflow Exception?
Posted on 1997-12-11
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:
unsigned long value = 4294967293;
for( int i = 0; i < 5; i++ )
value = value + 1;
__except( ( GetExceptionCode() ==
EXCEPTION_INT_OVERFLOW ) ? EXCEPTION_EXECUTE_HANDLER :
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:
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.