Returns nil or the raised exception·Updated May 16, 2026·Kernel Methods
exceptionserrorscontrol-flow
raise is a Kernel method that triggers an exception, interrupting the normal program flow. When an exception is raised, Ruby searches up the call stack for a rescue block that can handle it. If no handler is found, the program terminates and displays the exception message along with a backtrace. This mechanism is fundamental to Ruby’s error handling and is used for everything from validating input to handling unexpected runtime conditions.
def set_age(age) raise ArgumentError, "Age must be a number" unless age.is_a?(Numeric) raise ArgumentError, "Age cannot be negative" if age < 0 @age = ageend
Re-raising Exceptions
begin # Some operation that might fail File.read("nonexistent.txt")rescue => e puts "Error occurred: #{e.message}" raise # Re-raises the same exceptionend
Custom Exception Classes
class ValidationError < StandardError; enddef validate!(input) raise ValidationError, "Input cannot be empty" if input.nil? || input.empty?end
Errors (Common Exception Types)
Exception
Description
RuntimeError
Generic error raised when no specific class is specified (default)