Search code examples
c#exceptionoverflowexception

Does C# throw OverflowException for floating point numbers?


Does C# compiler throw OverflowException for floating-point numeric types?

I tried this to figure it out:

try
{
    checked
    {
        double d = Convert.ToDouble(Math.Pow(double.MaxValue, double.MaxValue));
        Console.WriteLine(d);
    }
}
catch (OverflowException)
{
    throw;
}

and what I saw in the console window was an ∞.

Is ∞ more useful when debugging than an exception?


Solution

  • No, C# does not have exceptions for floating point operations.

    The floating point type has 3 special values: positive infinity, negative infinity, and "not a number".

    If the result of a calculation is greater than what can be represented, it overflows without an exception being thrown and the result is positive infinity. is how it's represented in a string.