report_on_exception
- 1_8_6_287
- 1_8_7_72
- 1_8_7_330
- 1_9_1_378
- 1_9_2_180
- 1_9_3_125
- 1_9_3_392
- 2_1_10
- 2_2_9
- 2_4_6 (0)
- 2_5_5 (38)
- 2_6_3 (0)
- What's this?
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=.