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
magentaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
Try changing the type of value from unsigned to signed to see what happens.
0
magentaAuthor Commented:
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...
0
fasterCommented:
Integer overflow exception?  I would be VERY surprised if there is anything like that in C/C++.

You know the compiler will simply compile your C/C++ code into something like that:

inc eax

or:

add eax, ebx

or something similar, and these instructions will never cause the CPU to have any "exception"!  If eax is already 0xffffffff, then the CPU will happily assign 0 to eax when you add 1 to it.  This is regarded as normal! (Sometimes I even make use of this feature when I write in assembly).

Therefore there is really no such kind of exception.  It is the responsibility of the programmer to ensure that it will not overflow, not the CPU (it is not designed in this way) or the compiler (just imagine how much overhead it will be if the compiler puts such check!)

Divided by 0 is absolutely different, because at the CPU level it will have a REAL exception (there is an interupt devoted to that), therefore you are able to catch it.

In short, you have to take care of this yourself.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

nietodCommented:
Correction.  No integer overflow exception at all.  Not even for the multiplication and division.  
0
nietodCommented:
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?
0
chensuCommented:
Hi niotod, don't forget I am "faster", want to change your name to "fastest"?
0
fasterCommented:
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.)
0
nietodCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.