2
I was learning about error handling in the Java language and did tests with the sample code of the class in question. In a test I added (double)
in the final operation to make the result more accurate and noticed that after that, when I tried to divide any number "x" by "0", instead of the error "Arithmeticexception" I received an "Infinity" as a result.
Why does this happen? And why this only occurs for operations with floating point numbers (since, with the operation on integers, the error normally occurred)?
Follows the code:
Scanner s = new Scanner(System.in);
boolean continua = true;
do {
try {
System.out.print("Numerador: ");
int a = s.nextInt();
System.out.print("Denominador: ");
int b = s.nextInt();
System.out.println("O resultado da divisão foi: " + (double) a / b);
s.close();
continua = false;
}
catch (InputMismatchException e1) {
System.err.println("Informe um número inteiro");
s.nextLine();
}
catch (ArithmeticException e2) {
System.err.println("Denominador deve ser diferente de zero");
s.nextLine();
}
} while (continua);
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.17.2
– TotallyUncool
No integer or real number can be divided by zero. This is not from programming logic, but from mathematics.
– Vinicius Fernandes
http://stackoverflow.com/a/2381633/6809703
– TotallyUncool