Why does the compiler not show an error when we try to divide a variable by zero

前端 未结 5 633
野的像风
野的像风 2021-01-06 08:20

If we try to run this code:

int d = 10/0;

We get a compiler error. So we cannot divide by zero.

Now consider this code:

<         


        
5条回答
  •  無奈伤痛
    2021-01-06 08:55

    The first:

    int d=10/0;
    

    will be resolved due compile time, because it is constant. The second will not be resolved because the compiler do only syntax checks, not math check.

提交回复
热议问题