Why catch block directly exeute

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.

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.

On Tue, Dec 7, 2010 at 11:08 AM, Durga B. [email protected]
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.

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

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
[1,2,3,4].each do |v|
throw(:finish,v) if v > 3
puts v
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:35 AM, Durga B. [email protected]
wrote:

Thank you very much Jess Gabriel y Galn but some confusion please
explain with different catch and throw,not include same both are same
block take as a different ,please.

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
http://phrogz.net/programmingruby/tut_exceptions.html) :

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

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

catch :quit do
name = promptAndGet "Name: "
age = promptAndGet "Age: "
sex = promptAndGet "Sex: "
#do_something_with name, age, sex
end

You can nest catch blocks:

catch(:first_level) do
puts “at first level”
catch(:second_level) do
puts “At second level”
if (rand(2) == 0)
throw :second_level
else
throw :first_level
end
puts “still in second level”
end
puts “still in first level”
end

Jesus.

On Dec 7, 9:08pm, “Durga B.” [email protected] 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/.

http://ruby.activeventure.com/programmingruby/book/tut_exceptions.html

http://ruby-doc.org/core/classes/Exception.html