Bug #7430

'unexpected return' occurs when call a Proc that returning value in rescue

Added by Danilo Coelho over 1 year ago. Updated over 1 year ago.

[ruby-core:50050]
Status:Rejected
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 1.9.3p286 (2012-10-12) [i386-mingw32] Backport:

Description

p = Proc.new do
begin
1 / 0
rescue
return "error handled by proc"
end
end

p.call

History

#1 Updated by Yusuke Endoh over 1 year 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

#2 Updated by Danilo Coelho over 1 year 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 |datestr|
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 |datestr|
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

Also available in: Atom PDF