Skip to content
Tutorials Center
My learning

Topics

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.

C#
int big = int.MaxValue;
int wrapped = big + 1;                 // -2147483648, no exception
int safe = checked(big + 1);           // throws OverflowException

Try it

C# Unchecked versus checked

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.

C# Library methods that always check

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

C#
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 / -1 throws OverflowException in every context, because the result does not exist as an int.
  • Constants are checked at compile time. int x = int.MaxValue + 1; does not compile (CS0220) unless wrapped in unchecked.
  • checked has 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.CreateSaturating clamps to the type's range, CreateTruncating keeps the low bits, CreateChecked throws.
  • Math.BigMul and widening to long avoid intermediate overflow in multiplications.
  • Related types: OverflowException derives from ArithmeticException, as does DivideByZeroException; catch the base type to handle both.
  • checked does not apply inside called methods. Only the operations written lexically inside the block are checked.