Solved

How to catch an Integer Overflow Exception?

Posted on 1997-12-11
8
3,612 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:magenta
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1409417
Try changing the type of value from unsigned to signed to see what happens.
0
 

Author Comment

by:magenta
ID: 1409418
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
 
LVL 7

Accepted Solution

by:
faster earned 200 total points
ID: 1409419
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
 
LVL 22

Expert Comment

by:nietod
ID: 1409420
Correction.  No integer overflow exception at all.  Not even for the multiplication and division.  
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:nietod
ID: 1409421
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
 
LVL 23

Expert Comment

by:chensu
ID: 1409422
Hi niotod, don't forget I am "faster", want to change your name to "fastest"?
0
 
LVL 7

Expert Comment

by:faster
ID: 1409423
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
 
LVL 22

Expert Comment

by:nietod
ID: 1409424
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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video discusses moving either the default database or any database to a new volume.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now