Bug #7430

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

Added by Danilo Coelho almost 3 years ago. Updated almost 3 years ago.

[ruby-core:50050]
Status:Rejected
Priority:Normal
Assignee:-
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 almost 3 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

#2 Updated by Danilo Coelho almost 3 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

Also available in: Atom PDF