< :前の番号
^ :番号順リスト
> :次の番号
P :前の記事(スレッド移動)
N :次の記事(スレッド移動)
|<:前のスレッド
>|:次のスレッド
^ :返事先
_:自分への返事
>:同じ返事先を持つ記事(前)
<:同じ返事先を持つ記事(後)
---:分割してスレッド表示、再表示
| :分割して(縦)スレッド表示、再表示
~ :スレッドのフレーム消去
.:インデックス
..:インデックスのインデックス
Issue #13930 has been reported by msauter (Michael Sauter).
----------------------------------------
Bug #13930: Exception is caught in rescue above ensure
https://bugs.ruby-lang.org/issues/13930
* Author: msauter (Michael Sauter)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
* ruby -v: 2.4.1p111 (2017-03-22 revision 58053) [x86_64-linux]
* Backport: 2.2: UNKNOWN, 2.3: UNKNOWN, 2.4: UNKNOWN
----------------------------------------
Given the following code:
~~~ ruby
def foo
i = 0
3.times do |n|
begin
puts "a"
i += 1
next if i > 1
puts "b"
rescue => e
puts "rescue in foo, caught #{e}"
ensure
puts "ensure in foo, yield from foo: #{n}"
yield n
end
end
end
begin
x = 0
foo do |o|
puts o
x += 1
raise "test" if x > 1
puts "done yielding"
end
rescue => e
puts "rescue outside, caught #{e}"
ensure
puts "ensure outside"
end
~~~
The output is:
~~~
a
b
ensure in foo, yield from foo: 0
0
done yielding
a
ensure in foo, yield from foo: 1
1
rescue in foo, caught test
ensure in foo, yield from foo: 1
1
rescue outside
ensure outside
~~~
So the exception raised within the yielded block is caught in the rescue block above the ensure block which yielded. That sounds wrong to me. Or is it intended? The issue seems to be with the usage of next.
If I change the code to this:
~~~ ruby
def foo
i = 0
3.times do |n|
begin
puts "a"
i += 1
# next if i > 1
puts "b"
rescue => e
puts "rescue in foo, caught #{e}"
ensure
puts "ensure in foo, yield from foo: #{n}"
yield n
end
end
end
begin
x = 0
foo do |o|
puts o
x += 1
raise "test" if x > 1
puts "done yielding"
end
rescue => e
puts "rescue outside, caught #{e}"
ensure
puts "ensure outside"
end
~~~
Then the output is:
~~~
a
b
ensure in foo, yield from foo: 0
0
done yielding
a
b
ensure in foo, yield from foo: 1
1
rescue outside
ensure outside
default@pjpp:/pjpp$ ruby foo.rb
a
b
ensure in foo, yield from foo: 0
0
done yielding
a
b
ensure in foo, yield from foo: 1
1
rescue outside, caught test
ensure outside
~~~
I would have expected this output also when using next as above.
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>