Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
Thread#raise
(?::String message, ?cause: ::Exception?) -> nil
(::_Exception, ?::_ToS message, ?::Array[::Thread::Backtrace::Location] | ::Array[::String] | nil backtrace, ?cause: ::Exception?) -> nil
raise(exception, message = exception.to_s, backtrace = nil, cause: $!)
raise(message = nil, cause: $!)
thread.c
View on GitHub ↗
static VALUE
thread_raise_m(int argc, VALUE *argv, VALUE self)
{
rb_thread_t *target_th = rb_thread_ptr(self);
const rb_thread_t *current_th = GET_THREAD();
threadptr_check_pending_interrupt_queue(target_th);
if (rb_threadptr_dead(target_th)) {
return Qnil;
}
VALUE exception = rb_exception_setup(argc, argv);
rb_threadptr_pending_interrupt_enque(target_th, exception);
rb_threadptr_interrupt(target_th);
/* To perform Thread.current.raise as Kernel.raise */
if (current_th == target_th) {
RUBY_VM_CHECK_INTS(target_th->ec);
}
return Qnil;
}
Raises an exception from the given thread. The caller does not have to be thr. See Kernel#raise for more information on arguments.
Example
Thread.abort_on_exception = true a = Thread.new { sleep(200) } a.raise("Gotcha")
This will produce:
prog.rb:3: Gotcha (RuntimeError) from prog.rb:2:in `initialize' from prog.rb:2:in `new' from prog.rb:2
Ruby version changes
-
4.0
-
Introduce support for
Thread#raise(cause:)argument similar toKernel#raise. [Feature #21360]
-
See also
#priority=
#report_on_exception
#priority
#report_on_exception=
also in
4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0