Check if a double is infinite in Java

后端 未结 7 480
深忆病人
深忆病人 2020-12-10 10:22

I\'m making a simple calculator for this homework, and Java is returning \"Infinity\" when divided by 0.

I need to display some error message, when I get infinity. T

相关标签:
7条回答
  • 2020-12-10 11:07

    This kind of errors are called exceptions. You can use try-catch block to catch this exception.

     try{
          result = 4/0;
     }
     catch(ArithmeticException e){
         System.out.println("You divided by zero");
     }
    

    you can read about exception handling here.

    0 讨论(0)
提交回复
热议问题