Bug #7430
closed'unexpected return' occurs when call a Proc that returning value in rescue
Description
p = Proc.new do
begin
1 / 0
rescue
return "error handled by proc"
end
end
p.call
Updated by mame (Yusuke Endoh) over 11 years ago
- Status changed from Open to Rejected
This is the spec. To exit from Proc, use next instead of return.
p = Proc.new do
begin
1 / 0
rescue
next "error handled by proc"
end
end
p.call
You can use return to exit from lambda.
p = lambda do
begin
1 / 0
rescue
return "error handled by proc"
end
end
p.call
--
Yusuke Endoh mame@tsg.ne.jp
Updated by danilo.s.coelho (Danilo Coelho) over 11 years ago
In fact, there is no problem with 'return' in 'rescue'.
My intention is to return after the Proc not continue execution. Like this:
def test
puts "start: test"
p = Proc.new do |date_str|
begin
require "time"
Time.parse(date_str)
puts "date #{date_str} is valid"
rescue Exception => e
return "error handled by proc: #{e}"
end
end
p.call("01/01/2012")
p.call("80/01/2012")
puts "finish: test"
end
test
But just does not work:
def validator
Proc.new do |date_str|
require "time"
begin
Time.parse(date_str)
puts "date #{date_str} is valid"
rescue Exception => e
return "error handled by proc: #{e}"
end
end
end
def test
puts "start: test"
v = validator
v.call("01/01/2012")
v.call("81/01/2012")
puts "finish: test"
end
test