How to get ruby ​​to output an exception from a thread?

When I create a thread with Thread.new {}, it looks like any exception that occurs in this thread never sees the light of day, and the application just calmly ignores it.

+7
multithreading ruby exception-handling
source share
2 answers

Usually threads are isolated from each other, so an exception in one will not lead to the end of the entire application.

But, although I never used them, the Thread class has several abort_on_exception methods, even with some examples. They should do what you want.
http://corelib.rubyonrails.org/classes/Thread.html

+3
source share

By adding to Nikita's answer, you can also throw an exception by calling thread.join in the thread you created.

If you run the program with the debug flag ( ruby -d ), you will also abort when an unhandled exception is thrown in the thread.

+3
source share

All Articles