Division by zero
Type 5 ÷ 0 on a calculator. No number appears on the screen: the calculator shows an error and refuses to go on until you reset it. You cannot divide by zero, and the calculator tells you so at once.
In C# you cannot divide by zero either, but what happens next depends on the type of the numbers. Integers and decimal stop the program, while double silently gives a special value and keeps computing.
Integers
Dividing an int by a variable that holds zero throws an exception, DivideByZeroException. An exception is a signal of an error at run time. The program prints a message about it and stops at that line:
int zero = 0;
Console.WriteLine("Start"); // Start
Console.WriteLine(10 / zero); // ✗
Console.WriteLine("End");Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.Let's go through the program line by line:
int zero = 0;puts zero into the variablezero.- The first call to
Console.WriteLineprintsStart. 10 / zerothrows the exception, and the program stops.- The last line is never reached, so
Endis not printed.
Here C# behaves like the calculator: it reports the error and computes nothing more.
A program that divides an int by the literal 0 itself does not even compile. The compiler sees the zero in advance and reports error CS0020: Division by constant zero. That is why the example puts the zero into a variable first: the compiler does not compute a variable's value in advance.
double
Dividing a double by zero does not stop the program. 1.0 / 0 gives infinity, a special value of the double type, and 0.0 / 0 gives NaN, "not a number". The program goes on computing with these values, so the error can go unnoticed.
This is where the calculator analogy ends. A calculator shows an error for any division by zero, while double reports nothing.
decimal
decimal behaves like the integers: dividing by zero throws DivideByZeroException. Money kept in decimal never silently turns into infinity.
Interviewers like to ask this: what happens when you divide by zero in C#? A good answer tells the types apart:
intanddecimalthrow aDivideByZeroException;doublegives infinity orNaNand does not stop the program.
Common mistake
An average is computed in double, and nobody checks that the count can be zero:
double total = 0;
int count = 0;
double average = total / count; // ✗
Console.WriteLine(average); // NaNThe program does not stop and prints NaN. The dividend total has the type double, so 0.0 / 0 gives "not a number", not an exception. From here NaN gets into every calculation with the average, and you have to hunt for the error far from this line. With Russian regional settings the program prints не число.
Check the divisor before dividing: dividing a double by zero throws no exception that would stop the program.
Key points
- Dividing an
intor adecimalby zero throws aDivideByZeroException, and the program stops. - Dividing an
intby the literal0does not compile: errorCS0020. - Dividing a
doubleby zero gives infinity orNaNand does not stop the program.