I think that the following code should crash because I am performing an integer division by zero:
int n = 42;
eatCake(n / 0);
Why does dividing by zero not crash my app?
The key here is that n / 0
is not integer division.
Dart automatically performs double
division and the double
type has the double.infinity
constant. This means that print(n / 0)
will yield Infinity
.
Furthermore, double.infinity
is actually defined as 1.0 / 0.0
.
If you use integer division instead, you will receive an IntegerDivisionByZeroException
:
print(n ~/ 0); // <- IntegerDivisionByZeroException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With