Csharp 编程中的 DivideByZeroException

DivideByZeroExeption in Csharp Programing

我正在尝试在 C# 中处理 DivideByZeroExeption 但代码没有捕获异常,10 / d 的控制台打印结果是 ∞

            double d = 0;
            try
            {
                double value = 10 / d;
                Console.WriteLine(value);
            }
            catch (DivideByZeroException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine("Ignore...");
            }

enter image description here 但是当我将 d 的类型从 double 更改为 intlong,上面这段代码正常工作。

 int test = 0;
        try
        {
            double value = 10 / test;
            Console.WriteLine(value);
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine("Ignore...");
        }

Now this code will give you the error which you are expecting. It is treating d as something else. DivideByZeroException comes only in case of integer.