スレッド内の例外をすぐにキャッチしてすべてのスレッドを停止する必要があるため、スクリプトでabort_on_exceptionを使用しています。残念ながら、これは例外が親スレッドに発生しないことを意味します-おそらくこれは、例外がグローバルスコープで発生することになったためですか?
とにかく、これが問題を示す例です:
Thread.abort_on_exception = true
begin
t = Thread.new {
puts "Start thread"
raise saveMe
puts "Never here.."
}
t.join
rescue => e
puts "RESCUE: #{e}"
ensure
puts "ENSURE"
end
abort_on_exceptionを使用しているときにスレッドで発生した例外をどのように救済できますか?
これは、さらに厄介なことを示す新しい例です。スレッドはbeginブロック内の実行を強制終了できますが、例外を発生させることなく実行を強制終了しますか?
Thread.abort_on_exception = true
begin
t = Thread.new { raise saveMe }
sleep 1
puts "This doesn't execute"
rescue => e
puts "This also doesn't execute"
ensure
puts "But this does??"
end