I have written the code below. And I want the output to be :Error!Division by zero.. But my output is: infinity. What's the problem with this code?
//This is the class:
public class Exc
{
private double number1;
private double number2;
public double result;
public Exc(double n1,double n2)
{
this.number1 = n1;
this.number2 = n2;
}
public void Div(double number1, double number2)
{
try
{
result = number1 / number2;
Console.WriteLine(result);
}
catch (DivideByZeroException e)
{
Console.WriteLine("Error! Division by zero.{0}",e);
}
}
}
//This is my program:
static void Main(string[] args)
{
Console.WriteLine("Enter the first number:");
double n1 = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("Enter the second number:");
double n2 = Convert.ToDouble(Console.ReadLine());
Exc obj = new Exc(n1, n2);
obj.Div(n1,n2);
Console.ReadKey();
}