I have been researching this question to add it to the proposal for
integer overflow definition in C. Please add to the languages listed
below or correct me if I am wrong.
Java.
Wrap around. No special detection mechanism, as C now
C#
it has a "checked" construct that throws an exception on overflow:
using System;
class Overflow
{
static void Main()
{
try
{
int x = int.MaxValue + 1; // wraps to int.MinValue
int y = checked(int.MaxValue + 1); // throws
}
catch (OverflowException caught)
{
Console.WriteLine(caught);
}
}
}
FORTRAN
Compiler specific, apparently no language support.
This is the documentation from the HP forran (and C)
compilers:
http://docs.hp.com/en/B3906-90006/ch06s04.html
<quote>
To enable integer overflow checking for Fortran, use a !$HP$
CHECK_OVERFLOW INTEGER ON directive (in HP Fortran/9000, use
$CHECK_OVERFLOW INTEGER_4 or INTEGER_2) to obtain the overflow checking
code, and use an ON INTEGER OVERFLOW statement to handle the trap. (The
!$HP$ CHECK_OVERFLOW directive does not enable checking for operations
in libraries. Using the exponentiation operator involves a library call
in HP Fortran, so it is not possible to enable integer overflow checking
for exponentiation operations.)
There is no way to enable integer overflow checking in C. HP C provides
no mechanism to insert overflow checking code into your executable,
because the C language does not define integer overflow as an error.
<end quote>
Note that they say that integer overflow is not an error in C,
when in fact it is.
Python
Converts overflowing integers into longs and goes on. Of course this
is not so easy in compiled code.
Ada
Traps on integer overflow
Haskell
Uses IntS and IntegerS. Ints are like C ints, but IntegerS go smoothly
without overflowing.
Ruby
Undefined, there is no available specs about that. Since ruby is
written in C, the behavior is probably the same as C.