# Does Crystal's exception handling follow a different logic from Ruby's?

**URL:** <https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101>\
**Category:** Help & Support\
**Created:** [November 28, 2021, 2:35pm UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101 "2021-11-28T14:35:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [November 28, 2021, 2:35pm UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101/1 "2021-11-28T14:35:05Z")

</div>

```auto
begin
    a = 1/0
rescue 
    puts "Do not divide by zero"
    a = :bad_result
end

puts "After exception handling"
puts a

```

Ruby:

```auto
After exception handling
bad_result

```

Crystal:

```auto
After exception handling
Infinity

```

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [November 28, 2021, 2:55pm UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101/2 "2021-11-28T14:55:11Z")

</div>

No, exception handling is the same. What’s different is that division by default is float division. It never raises.

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [November 28, 2021, 2:56pm UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101/3 "2021-11-28T14:56:56Z")

</div>

I see.Thanks!

---

<div class="post-metadata">

**Author:** ![serge-hulne](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/serge-hulne/32/1211_2.png) [@serge-hulne](https://forum.crystal-lang.org/u/serge-hulne)\
**Post date:** [November 28, 2021, 3:05pm UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101/4 "2021-11-28T15:05:54Z")

</div>

This snippet is a better example:

```auto
begin
    File.open("Googoo.txt")
rescue e 
    puts e
    puts "Error occured in fle: #{ __FILE__ } on line #{ __LINE__ }"
end

```

---

<div class="post-metadata">

**Author:** ![mgomes](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/mgomes/32/1621_2.png) [@mgomes](https://forum.crystal-lang.org/u/mgomes)\
**Post date:** [November 29, 2021, 4:18am UTC](https://forum.crystal-lang.org/t/does-crystals-exception-handling-follow-a-different-logic-from-rubys/4101/5 "2021-11-29T04:18:02Z")

</div>

The equivalent Crystal code from your original example would be:

```auto
begin
    a = 1//0
rescue 
    puts "Do not divide by zero"
    a = :bad_result
end

puts "After exception handling"
puts a

```

[Here’s the output](https://play.crystal-lang.org/#/r/ccwn).
