Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't java compiler show a warning for an obvious dividing by zero? [duplicate]

This is just my curiosity to know why java compiler doesn't show any warning in this case while it shows warning for unchecked operations to predict a probable ClassCastException.

public class DivideByZero {
    public static void main() {
        int number = 3/0;
        System.out.println(number);
    }
}

Above code is guaranteed to produce an ArithmeticException.

This is an example only. There are more ways to guarantee a runtime exception without any warning during compilation.

like image 914
coder11 Avatar asked Feb 13 '26 06:02

coder11


1 Answers

Because technically a divide by zero is a valid program. Compilers only determine the validity of the program not whether there will be a runtime problem. It would be technically incorrect for a compiler to not let you compile a program with a divide by zero since it's valid.

like image 172
Jesus Ramos Avatar answered Feb 15 '26 20:02

Jesus Ramos