1

Why is explicit division By zero not considered as compile time exception in JAVA ?

Example :

int x =9; int y = (int)x/0;

Shouldnt compiler notify me about it ?

I tried running the same and finding answers to it , I am not convinced that why is that a checked exception. Divinding by another variable has to be runtime , agreed. But explicit division by ) shoukd be at compile time.

Valerij Dobler
  • 1,848
  • 15
  • 25
  • 1
    Good question. This could definitely be something interesting. At least as a warning. – Yassin Hajaj Jun 04 '23 at 18:46
  • 1
    Well, there are IntelliJ inspections for that, and you can set it to error if you like. And SonarLint is also generating a warning for it. I'm sure that spotbugs and errorprone also detect that. – Valerij Dobler Jun 04 '23 at 19:06
  • 1
    I guess it is a very naive bug to write hence it will always fail and that is why it is not handled by the compiler. – Valerij Dobler Jun 04 '23 at 19:06

0 Answers0