# Jump Out Of A Nested Context With Throw/Catch

Ruby's `throw/catch` construct, not to be confused with its `raise/rescue` exception handling syntax, allows you to jump out of a nested context. This is similar to [loop labels](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/label) in other languages.

For example, in my recent [Advent of Code solution](https://www.youtube.com/watch?v=Hvp07gTQhF4), I was able to employ this construct. Once within a doubly-nested loop, I can `throw` when I find the answer I'm looking for to both break out of the loop and return an value.

```ruby
answer =
  catch do |obj|
    input.each_with_index do |input1, x|
      input.each_with_index do |input2, y|
        next unless x != y

        next unless input1 + input2 == 2020

        throw(obj, input1 * input2)
      end
    end

    raise StandardError, 'No answer found'
  end

puts answer
```

If I were to never reach the `throw` before exhausting the doubly-nested loop, then the catch would product whatever value is returned within the block. In this case, I raise an error because it'd be exceptional for the `throw` to never be reached.

[source](https://apidock.com/ruby/Kernel/catch)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/ruby/jump-out-of-a-nested-context-with-throw-catch.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
