Solved

How to catch an Integer Overflow Exception?

Posted on 1997-12-11
8
3,678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 22

Expert Comment

by:nietod
ID: 1409420
Correction.  No integer overflow exception at all.  Not even for the multiplication and division.  
0
 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…

735 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