UncaughtThrowError
RubyERRORNotableControl Flow
throw called with no matching catch
Quick Answer
Ensure every throw(:symbol) has a corresponding catch(:symbol) enclosing it in the call stack.
What this means
Raised when throw is called with a symbol for which there is no enclosing matching catch block. throw/catch in Ruby are a non-exception control flow mechanism, distinct from raise/rescue.
Why it happens
- 1throw :done called outside any catch :done block
- 2Mismatched throw/catch symbol names (typo)
Fix
Wrap with matching catch
Wrap with matching catch
result = catch(:found) do
data.each do |item|
throw :found, item if item.matches?(query)
end
nil # default if not found
endWhy this works
catch(:found) creates the landing point for throw :found, returning the thrown value.
Code examples
Reproducing the errorruby
throw :done # UncaughtThrowError: uncaught throw :done
Correct catch/throw usageruby
catch(:exit) do
[1,2,3].each { |n| throw :exit, n if n == 2 }
end # => 2Rescue UncaughtThrowErrorruby
begin
throw :missing_catch
rescue UncaughtThrowError => e
puts "No catch for: #{e.tag}"
endSame error in other languages
Sources
Official documentation ↗
Ruby Core Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev