Search code examples
rubyerror-handlingrescue

rescue that calls a method with a rescue


This only prints rescue 1, is there a way to print both rescue 1 and rescue 2?

def mimiti
  raise 'hi there!'
rescue
  puts 'rescue 1'
end

begin
  mimiti
rescue
  puts 'rescue 2'
end

Solution

  • Yes, you can re-raise an exception after catching and handling it:

    def mimiti
      raise 'hi there!'
    rescue StandardError => e
      puts 'rescue 1'
      raise e
    end