The exception that is thrown when there is an attempt to divide an integral or decimal value by zero.
See Also: DivideByZeroException Members
Trying to divide an integer or decimal number by zero throws a DivideByZeroException exception. Dividing a floating-point value by zero doesn't throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic. Because the following example uses floating-point division rather than integer division, the operation does not throw a DivideByZeroException exception.
code reference: System.DivideByZeroException.Class#2
For more information, see float and double.
The following Microsoft intermediate language (MSIL) instructions throw DivideByZeroException:
div
div.un
rem
rem.un
DivideByZeroException uses the HRESULT COR_E_DIVIDEBYZERO, which has the value 0x80020012.
For a list of initial property values for an instance of DivideByZeroException, see the DivideByZeroException.#ctor constructors.
The following example demonstrates an error that causes a DivideByZeroException exception.
C# Example
using System; public class DivideZeroTest { public static void Main() { int x = 0; try { int y = 100/x; } catch (DivideByZeroException e) { Console.WriteLine("Error: {0}",e); } } }
The output is
Example
Error: System.DivideByZeroException: Attempted to divide by zero. at DivideZeroTest.Main()