How can I capture an exception from another thread?
- I want to do this for some particular thread, and not for other threads, so I cannot turn on
abort_on_exceptiontotrue. - I want to do something in the main thread with the exception, so I cannot simply do
puts,pp, etc. with the exception within the thread that raised in exception.
I found an answer in How to get error messages from ruby threads that suggests using catch, and I think that is the way I want to go, but I cannot not fully figure out how to do it. Or is there a better way? I also found an answer suggesting to use Queue class, but have concern that it may be overkill.
If you turn on
abort_on_exceptionthen you won’t have a chance to catch it. You can, however, leave that off and simply catch it when you do thejoinoperation on your thread.The alternative is to make the thread catch its own exception and save it somewhere you can fetch it from later. For instance: