C# Reference ยท Exceptions and Diagnostics
OverflowException
Reference for OverflowException in C#: checked arithmetic, int.Parse range errors, Math.Abs(int.MinValue), unchecked wraparound and safe alternatives.
OverflowException is thrown when a calculation or conversion produces a value that does not fit in the target type. By default, C# integer arithmetic wraps around silently (unchecked context). The exception appears only when overflow checking is on, for example inside a checked block, or when a library method always checks, such as Convert.ToInt32(long), int.Parse for out-of-range text or Math.Abs(int.MinValue).
Silent wraparound is often the worse outcome, since the program continues with a wrong number. Use checked for money, sizes, indexes and anything where a wrong value is unacceptable.
int big = int.MaxValue; int wrapped = big + 1; // -2147483648, no exception int safe = checked(big + 1); // throws OverflowException
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public class OverflowException : ArithmeticException { public OverflowException(); public OverflowException(string? message); public OverflowException(string? message, Exception? innerException); } checked(expression) // expression form checked { statements } // block form unchecked(expression)
| Context | Behavior on overflow |
|---|---|
Default integer arithmetic (+ - * ++ --, casts) |
Wraps around. Constant expressions that overflow are compile errors (CS0220). |
checked |
Throws OverflowException. |
unchecked |
Wraps around, even if the project enables checking. |
float, double |
Becomes Infinity or NaN; no exception. |
decimal |
Always throws OverflowException. |
BigInteger |
Never overflows (grows as needed). |
Usage notes
- Compiler option.
<CheckForOverflowUnderflow>turns on checking project-wide. Our runner leaves it off, which is the default. - Division edge case.
int.MinValue / -1throwsOverflowExceptionin every context, because the result does not exist as anint. - Constants are checked at compile time.
int x = int.MaxValue + 1;does not compile (CS0220) unless wrapped inunchecked. checkedhas a small cost (an extra branch per operation), so profile before enabling it everywhere in hot loops.- Math helpers.
Math.Abs,Math.Round,Convert.To...and parsing methods always check, regardless of context. - Saturating and truncating.
T.CreateSaturatingclamps to the type's range,CreateTruncatingkeeps the low bits,CreateCheckedthrows. Math.BigMuland widening tolongavoid intermediate overflow in multiplications.- Related types:
OverflowExceptionderives fromArithmeticException, as doesDivideByZeroException; catch the base type to handle both. checkeddoes not apply inside called methods. Only the operations written lexically inside the block are checked.