Flowdock
method

report_on_exception

Importance_2
v2_6_3 - Show latest stable - 0 notes - Class: Thread
report_on_exception() public

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 Thread will be joined with Thread#join or Thread#value, then it is safe to disable this report with Thread.current.report_on_exception = false when starting the Thread. However, this might handle the exception much later, or not at all if the Thread is 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=.

Show source
Register or log in to add new notes.