Skip to content
Theory · Operators · lesson 8 of 10

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:

C#
int zero = 0;
Console.WriteLine("Start"); // Start
Console.WriteLine(10 / zero); // ✗
Console.WriteLine("End");
TEXT
Unhandled exception. System.DivideByZeroException: Attempted to divide by zero.

Let's go through the program line by line:

  1. int zero = 0; puts zero into the variable zero.
  2. The first call to Console.WriteLine prints Start.
  3. 10 / zero throws the exception, and the program stops.
  4. The last line is never reached, so End is 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:

  • int and decimal throw a DivideByZeroException;
  • double gives infinity or NaN and does not stop the program.

Common mistake

An average is computed in double, and nobody checks that the count can be zero:

C#
double total = 0;
int count = 0;
double average = total / count; // ✗
Console.WriteLine(average); // NaN

The 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 int or a decimal by zero throws a DivideByZeroException, and the program stops.
  • Dividing an int by the literal 0 does not compile: error CS0020.
  • Dividing a double by zero gives infinity or NaN and does not stop the program.