Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Fiber#raise

(?::string msg, ?cause: ::Exception?) -> untyped
(::_Exception, ?::string msg, ?::Array[::string] | ::Array[::Thread::Backtrace::Location] | nil backtrace, ?cause: ::Exception?) -> untyped
raise(exception, message = exception.to_s, backtrace = nil, cause: $!)
raise(message = nil, cause: $!)

Raises an exception in the fiber at the point at which the last Fiber.yield was called.

Example
f = Fiber.new {
  puts "Before the yield"
  Fiber.yield 1 # -- exception will be raised here
  puts "After the yield"
}

p f.resume
f.raise "Gotcha"

Output

Before the first yield
1
t.rb:8:in 'Fiber.yield': Gotcha (RuntimeError)
  from t.rb:8:in 'block in <main>'

If the fiber has not been started or has already run to completion, raises FiberError. If the fiber is yielding, it is resumed. If it is transferring, it is transferred into. But if it is resuming, raises FiberError.

Raises FiberError if called on a Fiber belonging to another Thread.

See Kernel#raise for more information on arguments.

Ruby version changes

  1. 4.0
See also #kill #resume #inspect #storage also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0