0

I am trying to handle DivideByZeroExeption in C# but the code do not catch the exeption, the Console print result of 10 / d is ∞

            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 But when I changed type of d from double to int or long, this code above works normally.

Tung Ng.
  • 1,241
  • 2
  • 8
  • 12
  • Possible duplicate of https://stackoverflow.com/questions/44258124/divide-by-zero-and-no-error/44258269 – krmckone Oct 23 '19 at 14:28

1 Answers1

1
 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.