【发布时间】:2014-11-11 00:27:26
【问题描述】:
Thread.abort_on_exception = true
begin
#code in this block doesn't matter at all, it just needs to produce errors.
[1, 2].each do |i|
a = Thread.new do
errory
end
end
a.join
rescue Exception
puts 'error!'
end
由于某种原因引发/home/lakesare/Desktop/multithreading/test.rb:6:in 'block (2 levels) in <main>': undefined local variable or method 'errory' for main:Object (NameError) 而不是返回error!。
如果Thread.new {} 没有包裹在each {} 中,则块被正确救出。
这是为什么?在这种情况下如何正确拯救我的线程?
编辑:我发现将 begin-rescue-end 块包装在相同的块中会有所帮助。但是一个问题仍然存在——为什么一次救援还不够?
编辑:包装在另一个救援块中会有所帮助,但并非总是如此 - 有时它仍然无法救援。
【问题讨论】:
标签: ruby multithreading rescue