Ruby Language Control Flow throw, catch


Example

Unlike many other programming languages, the throw and catch keywords are not related to exception handling in Ruby.

In Ruby, throw and catch act a bit like labels in other languages. They are used to change the control flow, but are not related to a concept of "error" like Exceptions are.

catch(:out) do
  catch(:nested) do
    puts "nested"
  end

  puts "before"
  throw :out
  puts "will not be executed"
end
puts "after"
# prints "nested", "before", "after"