我需要立即捕捉線程中的異常並停止所有線程,所以我在我的腳本中使用abort_on_exception。不幸的是,這意味着異常不會引發到父線程 - 也許這是因爲異常最終發生在全局範圍內?紅寶石不能救援或看到中止Thread.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時在線程引發該異常?
下面是一個新的例子,顯示更令人驚訝的事情。該線程能夠在開始塊內終止執行,但它不會引發任何異常?
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
這是不是由於它所指的範圍爲例外全球。您將需要父進程需要「照顧」子進程。檢查線程文檔。 – vgoff
從[Pickaxe](http://www.rubycentral.com/pickaxe/tut_threads.html)(可能會有點過時,但應該引導您朝着正確的方向。) – vgoff
vgoff:鎬手冊沒有除了說出abort_on_exception的功能外,還有任何信息,我已經知道了。 我不知道你的意思是「保姆」 - 你能詳細說明一下嗎?我已經閱讀了線程文檔,並且使用了相當多的線程,但是我不明白如何在這種情況下將錯誤提交到線程之外。 –