Ruby : Not throwing exception even when divided by Zero : Only when operands are floating points

# Throws an Exception when both operands are integers
# However, if operand are floating point numbers then that is not the case
begin
 puts 'Enter the Number'
 quotient = gets.to_i
 puts 'enter the divider'
 divider = gets.to_i
 result = quotient/divider
 puts "The result is #{result}"
rescue ZeroDivisionError
 puts 'Cannot divide by zero'
 retry
end
# Output
Enter the Number
12
enter the divider
0
Cannot divide by zero
# Whole process within begin block retried
Enter the Number
# However
begin
 puts 'Enter the Number'
 quotient = gets.to_f
 puts 'enter the divider'
 divider = gets.to_f
 result = quotient/divider
 puts "The result is #{result}"
rescue ZeroDivisionError
 puts 'Cannot divide by zero'
 retry
end
# Output
Enter the Number
12
enter the divider
0
The result is Infinity

12.12/0.0
=> Infinity

# However
12/0
ZeroDivisionError: divided by 0

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s