# Why catch block directly exeute

**URL:** <https://rubytalk.org/t/why-catch-block-directly-exeute/61072>\
**Category:** ruby-talk\
**Created:** [7 December 2010 10:08 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072 "2010-12-07T10:08:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Durga\_B](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Durga\_B](https://rubytalk.org/u/Durga_B)\
**Post date:** [7 December 2010 10:08 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072/1 "2010-12-07T10:08:38Z")

</div>

catch (:finish) do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Generated 1000 random numbers without generating 123!"  
&nbsp;&nbsp;&nbsp;&nbsp;end

The above code if not throw or any thing call and execute directly .  
How is it works please any one explain and if possible give a example  
for throw and catch block.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [7 December 2010 10:27 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072/2 "2010-12-07T10:27:17Z")

</div>

[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_m\_kernel.html#Kernel.catch](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.catch)  
[http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref\_m\_kernel.html#Kernel.throw](http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_m_kernel.html#Kernel.throw)

It's how catch works. It executes the block. If within the block, the  
symbol you pass to catch is thrown, catch returns the value passed to  
throw (second argument or nil). If not thrown, catch terminates  
normally, and returns the value of the last expression in the block:

value = catch(:finish) do  
&nbsp;&nbsp;[1,2,3,4].each do |v|  
&nbsp;&nbsp;&nbsp;&nbsp;throw(:finish,v) if v \> 3  
&nbsp;&nbsp;&nbsp;&nbsp;puts v  
&nbsp;&nbsp;end  
end

irb(main):019:0\> value = catch(:finish) do  
irb(main):020:1\* [1,2,3,4].each do |v|  
irb(main):021:2\* throw(:finish, v) if v \> 3  
irb(main):022:2\> puts v  
irb(main):023:2\> end  
irb(main):024:1\> end  
1  
2  
3  
=\> 4

If not thrown:

irb(main):025:0\> value = catch(:finish) do  
irb(main):026:1\* [1,2,3,4].each do |v|  
irb(main):027:2\* throw(:finish, v) if v \> 10  
irb(main):028:2\> puts v  
irb(main):029:2\> end  
irb(main):030:1\> end  
1  
2  
3  
4  
=\> [1, 2, 3, 4]

Returns the full array, which is what the each method returns.

Jesus.

Jesus.

> **···**
>
> On Tue, Dec 7, 2010 at 11:08 AM, Durga B. \<bdurgaprasad\_nyros@yahoo.com\> wrote:
> 
> > catch (:finish) do  
> > puts "Generated 1000 random numbers without generating 123!"  
> > end
> > 
> > The above code if not throw or any thing call and execute directly .  
> > How is it works please any one explain and if possible give a example  
> > for throw and catch block.

---

<div class="post-metadata">

**Author:** ![flebber](https://avatars.discourse-cdn.com/v4/letter/f/f9ae1b/32.png) [@flebber](https://rubytalk.org/u/flebber)\
**Post date:** [7 December 2010 10:55 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072/3 "2010-12-07T10:55:17Z")

</div>

[http://ruby.activeventure.com/programmingruby/book/tut\_exceptions.html](http://ruby.activeventure.com/programmingruby/book/tut_exceptions.html)

[http://ruby-doc.org/core/classes/Exception.html](http://ruby-doc.org/core/classes/Exception.html)

> <https://stackoverflow.com/questions/2018137/where-are-catch-and-throw-useful-in-ruby>

> **···**
>
> On Dec 7, 9:08 pm, "Durga B." \<bdurgaprasad\_ny...@yahoo.com\> wrote:
> 
> > catch (:finish) do  
> > puts "Generated 1000 random numbers without generating 123!"  
> > end
> > 
> > The above code if not throw or any thing call and execute directly .  
> > How is it works please any one explain and if possible give a example  
> > for throw and catch block.
> > 
> > --  
> > Posted viahttp://www.ruby-forum.com/.

---

<div class="post-metadata">

**Author:** ![Durga\_B](https://avatars.discourse-cdn.com/v4/letter/d/9d8465/32.png) [@Durga\_B](https://rubytalk.org/u/Durga_B)\
**Post date:** [7 December 2010 10:35 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072/4 "2010-12-07T10:35:08Z")

</div>

Thank you very much Jesús Gabriel y Galán but some confusion please  
explain with different catch and throw,not include same both are same  
block take as a different ,please.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Jesus\_Gabriel\_y\_Gala](https://avatars.discourse-cdn.com/v4/letter/j/96bed5/32.png) [@Jesus\_Gabriel\_y\_Gala](https://rubytalk.org/u/Jesus_Gabriel_y_Gala)\
**Post date:** [7 December 2010 10:57 UTC](https://rubytalk.org/t/why-catch-block-directly-exeute/61072/5 "2010-12-07T10:57:35Z")

</div>

I'm not sure I understand your question. The way it works is that when  
you throw, Ruby looks up through the stack for a catch with the same  
symbol. If it finds it, the block passed to catch is terminated  
immediately, and the block returns the value passed to throw. This is  
useful to exit from a deeply nested structure, for example (based on  
[Exceptions, Catch, and Throw](http://phrogz.net/programmingruby/tut_exceptions.html%5C)) :

def processValue value  
&nbsp;&nbsp;throw :quit if value =~ /quit!/i  
&nbsp;&nbsp;value.downcase  
end

def promptAndGet(prompt)  
&nbsp;&nbsp;print prompt  
&nbsp;&nbsp;value = gets.chomp  
&nbsp;&nbsp;processValue value  
end

catch :quit do  
&nbsp;&nbsp;name = promptAndGet "Name: "  
&nbsp;&nbsp;age = promptAndGet "Age: "  
&nbsp;&nbsp;sex = promptAndGet "Sex: "  
&nbsp;&nbsp;#do\_something\_with name, age, sex  
end

You can nest catch blocks:

catch(:first\_level) do  
&nbsp;&nbsp;puts "at first level"  
&nbsp;&nbsp;catch(:second\_level) do  
&nbsp;&nbsp;&nbsp;&nbsp;puts "At second level"  
&nbsp;&nbsp;&nbsp;&nbsp;if (rand(2) == 0)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw :second\_level  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw :first\_level  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;puts "still in second level"  
&nbsp;&nbsp;end  
&nbsp;&nbsp;puts "still in first level"  
end

Jesus.

> **···**
>
> On Tue, Dec 7, 2010 at 11:35 AM, Durga B. \<bdurgaprasad\_nyros@yahoo.com\> wrote:
> 
> > Thank you very much Jesús Gabriel y Galán but some confusion please  
> > explain with different catch and throw,not include same both are same  
> > block take as a different ,please.
