Ruby Language Control Flow throw, catch


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"

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