DivideByZeroException Class
定义
命名空间:
System
Assemblies:
System.Runtime.dll, mscorlib.dll, netstandard.dll
尝试将整数或 Decimal 值除以零时引发的异常。
C#
复制
[System.Runtime.InteropServices.ComVisible(true)][System.Serializable]public class DivideByZeroException : ArithmeticException
继承
ObjectExceptionSystemExceptionArithmeticExceptionDivideByZeroException
属性
ComVisibleAttribute SerializableAttribute
示例
下面的示例处理DivideByZeroException整数除法中的异常。
C#
复制
using System;public class Example{ public static void Main() { int number1 = 3000; int number2 = 0; try { Console.WriteLine(number1 / number2); } catch (DivideByZeroException) { Console.WriteLine(“Division of {0} by zero.”, number1); } }}// The example displays the following output:// Division of 3000 by zero.
注解
尝试将整数或Decimal数字为零,则会引发DivideByZeroException异常。 若要避免此异常,请确保中使用整数除法运算的分母或Decimal值为非零。
浮点值除以 0 不会引发异常;这会导致正无穷大、 负无穷大或非数值 (NaN),根据 IEEE 754 算法的规则。 下面的示例使用浮点除法运算而不是整数除法,因为该操作不会引发DivideByZeroException异常。
C#
复制
using System;public class Example{ public static void Main() { int number1 = 3000; int number2 = 0; Console.WriteLine((double)number1 / number2); }}// The example displays the following output:
// Infinity