Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →
Thread.report_on_exception
static VALUE
rb_thread_s_report_exc(VALUE _)
{
return RBOOL(GET_THREAD()->vm->thread_report_on_exception);
}
Returns the status of the global “report on exception” condition.
The default is true since Ruby 2.5.
All threads created when this flag is true will report a message on $stderr if an exception kills the thread.
Thread.new { 1.times { raise } }
will produce this output on $stderr:
#<Thread:...> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
2: from -e:1:in `block in <main>'
1: from -e:1:in `times'
This is done to catch errors in threads early. In some cases, you might not want this output. There are multiple ways to avoid the extra output:
-
If the exception is not intended, the best is to fix the cause of the exception so it does not happen anymore.
-
If the exception is intended, it might be better to rescue it closer to where it is raised rather then let it kill the
Thread. -
If it is guaranteed the
Threadwill be joined withThread#joinorThread#value, then it is safe to disable this report withThread.current.report_on_exception = falsewhen starting theThread. However, this might handle the exception much later, or not at all if theThreadis never joined due to the parent thread being blocked, etc.
See also ::report_on_exception=.
There is also an instance level method to set this for a specific thread, see report_on_exception=.