Home > front end >  if-statement inside a try-catch block returns "Infinite"
if-statement inside a try-catch block returns "Infinite"

Time:11-14

First of all i'm new to programming and just finished the Course on SoloLearn about C#. I do understand the core principal of try-catch blocks but i can't figure out how to correctly implement this in to my code(code shown below).

class Program
{
    static void Main(string[] args)
    {   //create object of Class Calculator
        Calculator calc = new Calculator();

        //take user input and store in num1 and print it to screen
        try{
            double num1 = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nnumber1: "   num1);
            double num2 = Convert.ToInt32(Console.ReadLine());
            Console.Write("\nnumber2: "   num2);
        
            //take operator as input and print it to screen
            string operand = Console.ReadLine();
            Console.Write("\noperator: "   operand);

            //check if operator from user input is equal to one of the 4 calculation methods in Calculator Class
            Console.Write("\nresult: ");
            if(operand == " "){
            Console.WriteLine(calc.Addition(num1, num2));
            }
            else if(operand == "-"){
            Console.WriteLine(calc.Subtraction(num1, num2));
            }
            else if(operand == "*"){
            Console.WriteLine(calc.Multiplication(num1, num2));
            }
            else{
            Console.WriteLine(calc.Division(num1, num2));
            }
        }
        catch(DivideByZeroException){
            Console.WriteLine("can't divide by zero");
        }
        catch(Exception e){
            Console.WriteLine("An error occurred. Only integers are allowed!");
        } 
    }
}//class Calculator with methods for 4 simple calculations
class Calculator{
    private double number1;
    private double number2;
    private double res;

    public double Addition(double num1, double num2){
        this.number1 = num1;
        this.number2 = num2;
        this.res = num1   num2;
        return res;
    }
    public double Subtraction(double num1, double num2){
        this.number1 = num1;
        this.number2 = num2;
        this.res = num1 - num2;
        return res;
    }
    public double Multiplication(double num1, double num2){
        this.number1 = num1;
        this.number2 = num2;
        this.res = num1 * num2;
        return res;
    }
    public double Division(double num1, double num2){
        this.number1 = num1;
        this.number2 = num2;
        this.res = num1 / num2;
        return res;
    }
}

So i want my simple calculator to handle the exception "DivideByZero" and "Exception e" -> if the input was not an integer.

When i test the DivideByZero Exception with an example input of 4/0, the programm returns "Infinite" as a result instead of the code from the catch block.

I guess the "Infinite" result comes from the if-statements inside the try-catch block but i'm not sure.

I searched multiple sites, similar posts on stackoverflow and read the microsoft documentation for c# about try-catch blocks but i just can't figure it out.

Sorry if my code sample is too big but i think this is the best way to understand my mess of code.

Thank you in advance for the quick reply !

CodePudding user response:

There is nothing wrong with your try catch. If you divide a double by zero, you don't get an exception. You get Double.PositiveInfinity as result in your case. The DivideByZeroException is only thrown for integer or decimal data types.

CodePudding user response:

Change fragment

 else {
   Console.WriteLine(calc.Division(num1, num2));
 }

into

 else {
  Console.WriteLine(num2 != 0 
     ? $"{calc.Division(num1, num2)}" 
     :  "can't divide by zero");
}

Since floating point division doesn't throw exception but returns NaN, PositiveInfinity and NegativeInfinity:

 0.0 / 0.0 == double.NaN
 1.0 / 0.0 == double.PositiveInifinity
-1.0 / 0.0 == double.NegativeInfinity 
  • Related