Skip to content
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: $!)

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

  1. 4.0