Solved

How to catch an Integer Overflow Exception?

Posted on 1997-12-11
8
3,631 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MSDN Subscription - Azure and NFP's 3 64
Microsoft Query from QuickBooks 9 63
Problem to packaging 1 81
Problem to file 3 67
This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

914 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

21 Experts available now in Live!

Get 1:1 Help Now