Menu
Coddy logo textTech

ensure and raise

Part of the Logic & Flow section of Coddy's Ruby journey — lesson 46 of 56.

Two more pieces complete the error-handling toolkit.

ensure is a block that runs no matter what, whether begin finished normally, or a rescue branch caught an exception, or even if the exception was uncaught. It's where you put cleanup code:

begin
  puts "opening file"
  raise "something broke"
rescue
  puts "caught it"
ensure
  puts "closing file"
end
# opening file
# caught it
# closing file

raise is how you create your own exception. Pass a string for the message, or a class plus a message:

def withdraw(amount)
  raise ArgumentError, "amount must be positive" if amount <= 0
  # ... do the withdrawal
end

The convention is to raise early, at the top of a method, to fail fast when arguments are wrong. The caller can rescue if they have a sensible recovery, or let it bubble up.

challenge icon

Challenge

Easy

Define a method safe_divide(a, b) that:

  • Raises ArgumentError with the message "b cannot be zero" if b == 0
  • Otherwise returns a / b

Then read two integers from input. Call safe_divide inside begin / rescue / ensure:

  • On success, print Result: <value>
  • On ArgumentError, print Error: <message>
  • In ensure, always print Done.

For input 10 / 2, the output is:

Result: 5
Done.

For input 10 / 0:

Error: b cannot be zero
Done.

Cheat sheet

ensure runs no matter what (normal exit, rescued, or uncaught exception) — use it for cleanup:

begin
  raise "something broke"
rescue
  puts "caught it"
ensure
  puts "always runs"
end

raise creates an exception. Pass a class and message to raise a specific type:

raise ArgumentError, "amount must be positive" if amount <= 0

Convention: raise early at the top of a method to fail fast on bad arguments.

Try it yourself

# TODO: define safe_divide(a, b), raises ArgumentError when b == 0

a = gets.to_i
b = gets.to_i

# TODO: begin ... print Result; rescue ArgumentError => e ... print Error; ensure ... print Done.
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow