System.DivideByZeroException Class

The exception that is thrown when there is an attempt to divide an integral or decimal value by zero.

See Also: DivideByZeroException Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public class DivideByZeroException : ArithmeticException

Remarks

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:

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.

Thread Safety

All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.

Example

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()
 

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0