×

ruby · level 2 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

catches an error so your program can carry on.

When something goes wrong while a program runs, Ruby raises an . Until now, that crashed the program. Wrap the risky lines between and end, and add rescue before the end. If an exception happens, Ruby jumps straight to the rescue lines instead of crashing.

Write rescue => error to catch the exception in a variable. error.message is the message you would have seen, and error.class is its name.

begin
  puts "Opening the box..."
  puts 10 / 0
  puts "This line is skipped."
rescue => error
  puts "Oops: #{error.message}"
end
puts "Still running!"

Once an error happens, the rest of the lines inside begin are skipped.