Plough => Ruby

Journey through ruby

Nested Exceptions in Ruby 2.1.0

With Ruby 2.1.0, one can easily trace the original exception. Previously, on rescuing an exception one would have no reference to the original exception (thrown by a gem/library). There are a couple of gems that can help you keep track of the exceptions, but with Ruby 2.1.0 you can work with nested exceptions without any issues. Here’s some trivial code to achieve the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Car
  def self.start
    begin
      1/0
    rescue => ex
      puts "Exception: #{ex}"
      raise StandardError.new "Can't start the car"
    end
   end
end

begin
  Car.start
rescue => ex
  puts "Cause: #{ex.cause}"
  puts "Exception: #{ex}"
end

This will produce the following output:

1
2
3
Exception: divided by 0
Cause: divided by 0
Exception: Can't start the car

You can play around with the code yourself. It’s not as sophisticated as the gems out there, but it’s getting there.