Why is the exception not triggered for division by zero here?

后端 未结 2 1768
悲哀的现实
悲哀的现实 2021-02-19 06:29

(This is a follow up question to Why is this exception is not printed? Why is it showing an error?)

Here in the below code why is the ArithmeticException not tr

相关标签:
2条回答
  • 2021-02-19 06:44

    Because Divide by zero applies to integers and not floats as per JLS

    and you would get output as

    Its not gonna printed a=Infinity
    

    since this is computed as Infinity

    And in case you want to see an exception just change

    a=44/d;
    

    to this

    a=44/0;
    
    0 讨论(0)
  • 2021-02-19 06:53

    A division by zero throws an exception for integer values, but not for floating values. This is defined in the JLS #15.17.2:

    The result of a floating-point division is determined by the rules of IEEE 754 arithmetic:
    [...]

    • Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.

    If you change the type of a and d to int, you will get an exception.

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