Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#raise

raise
raise(string, cause: $!)
raise(exception [, string [, array]], cause: $!)

With no arguments, raises the exception in $! or raises a RuntimeError if $! is nil. With a single String argument, raises a RuntimeError with the string as a message. Otherwise, the first parameter should be an Exception class (or another object that returns an Exception object when sent an exception message). The optional second parameter sets the message associated with the exception (accessible via Exception#message), and the third parameter is an array of callback information (accessible via Exception#backtrace). The cause of the generated exception (accessible via Exception#cause) is automatically set to the “current” exception ($!), if any. An alternative value, either an Exception object or nil, can be specified via the :cause argument.

Exceptions are caught by the rescue clause of begin...end blocks.

Example
raise "Failed to create socket"
raise ArgumentError, "No parameters", caller
See also #puts #rand #putc #Rational also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1