Division by Zero

def div
begin
x = 20 / 0 # 10
rescue
pp "Division durch NULL ! "
end
end

x = div
pp x.to_s

Hi,

I have this little Program. I don’t understand why the rescue don’t work.

Compiling is ok.

Output for 20 / 10 is ok

Output for 20 / 0 is Infinity but not the pp “…”

Why?

(Sorry for my English)

/ is the float division and, under most circumstances, division by zero is fine for IEEE 754 floats. The integer division operator is // and will raise DivisionByZeroError.

1 Like