Divide by zero and no error? [duplicate]
You are going to have DivideByZeroException
only in case of integer values:
int total = 3;
int numberOf = 0;
var tot = total / numberOf; // DivideByZeroException thrown
If at least one argument is a floating point value (double
in the question) you'll have FloatingPointType.PositiveInfinity as a result (double.PositiveInfinity
in the context) and no exception
double total = 3.0;
int numberOf = 0;
var tot = total / numberOf; // tot is double, tot == double.PositiveInfinity
You may check like below
double total = 10.0;
double numberOf = 0.0;
var tot = total / numberOf;
// check for IsInfinity, IsPositiveInfinity,
// IsNegativeInfinity separately and take action appropriately if need be
if (double.IsInfinity(tot) ||
double.IsPositiveInfinity(tot) ||
double.IsNegativeInfinity(tot))
{
...
}