スレッドで発生した例外を捕捉する
マニュアル(http://docs.ruby-lang.org/ja/2.1.0/method/Thread/s/abort_on_exception.html)に書いてないけど、Thread.abort_on_exception = trueすると、スレッドで発生した例外がメインスレッドに飛んでくるようになるっぽい。
begin Thread.abort_on_exception = true Thread.new do raise 'hoge' end sleep rescue p $! # => '#<RuntimeError: hoge>' end
参考URL
- Handling exceptions raised in a Ruby thread, http://stackoverflow.com/questions/9095316/handling-exceptions-raised-in-a-ruby-thread