0

Maybe it's a foolish mistake but I can't understand: I'm making a method to get the average between some numbers read from a file. I've already handled the NumberFormatException in case my file contains non-numeric carachters,

        double sum = 0.0;
        double average = 0.0;
        ArrayList<String> list = getCollectionFromFile();
        for(String strValue : list) {
            try {
                double value = Double.parseDouble(strValue);
                sum+=value;
            }
            catch(NumberFormatException e) {
                System.out.println("Error! Non-numeric carachters detected!");
            }
        }

and then I tried to handle also the ArithmeticException in case my file is empty, so that the division should be 0.0/0

try {
            average = sum/getNRows();
        }
        catch(ArithmeticException e) {
            System.out.println("Error! File is empty! ArithmeticException!");
        }
        return average;
//method ends

But when I test my program calling average() on a empty file, it doesn't catch ArithmeticException but outputs "NaN". What should I do in order to catch something displaying me the file is empty, and inform the user it doesn't make sense/it's wrong to call average()?

AleMaffe
  • 49
  • 1
  • 7
  • 1
    Are you really sure the `ArithmeticException` you are trying to catch is thrown inside your `try` block? Do you have some more code before or after that block that might throw an `ArithmeticException`, too? Btw, using an `ArithmeticException` to check if the file is empty is awkward. Check the file size before to make your code look cleaner and easier to understand. – McPringle Jun 13 '21 at 09:06
  • @MarcusFihlon Wait that's not so awkward.. I know normally it's strange to do this, but this is logic: Division by zero = no lines = file is empty. What's strange? No, I don't have other code to catch ArithmeticException. Nice tip to check file size before making code – AleMaffe Jun 13 '21 at 09:12
  • 1
    Poor practice here. You should not rely on an unrelated exception to tell you the file is empty. You should check `list.size() explicitly`. – user207421 Jun 13 '21 at 10:12
  • @user207421 So I just can solve putting brackets around my code under the condition "if(!file.exists())" without throwing exception, ok, but if I WANT to throw (or directly catch something) to INFORM the user it's empty and no average calculation could be performed, what should I do? – AleMaffe Jun 13 '21 at 14:26

1 Answers1

2

The integer division 0/0 would throw an ArithmeticException but this would not happen if any of the operands are floating point numbers.

As Java uses IEEE754 floating point numbers, this division is allowed.

If zero is divided by zero (floating point division), the result is NaN (Not a Number) and this woukd not throw an exception.

If a positive non-zero number is divided by zero (or a negative non-zero) number is divided by negative zero), the result would be Infinity.

If a negative non-zero number is divided by zero (or a positive non-zero number is divided by negative zero), the result would be -Infinity.

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • Thank you so much! I thought to make a method which check if average is NaN: `public void checkNaN(double number) throws IllegalArgumentException { if(Double.isNaN(number) throw new IllegalArgumentException(); }` and then call it in my original method sorrounded with try-catch block catching IllegalArgumentException. You think it could be a good idea? – AleMaffe Jun 13 '21 at 09:16