advanced/exception_handling.rb

tests/_examples/advanced/exception_handling.rb in the metorex repository.


def risky_operation()
  puts 'risky operation!'
  raise 'Oops...'
end

def cleanup()
  puts 'cleanup'
end

begin
  risky_operation()
rescue StandardError => e
  puts "General error: #{e.message}"
ensure
  cleanup()
end

Output:

risky operation!
General error: Oops...
cleanup