Ruby Language Exceptions Raising an exception

30% OFF - 9th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY9


To raise an exception use Kernel#raise passing the exception class and/or message:

raise StandardError # raises a
raise StandardError, "An error" # raises a"An error")

You can also simply pass an error message. In this case, the message is wrapped into a RuntimeError:

raise "An error" # raises a"An error")

Here's an example:

def hello(subject)
  raise ArgumentError, "`subject` is missing" if subject.to_s.empty?
  puts "Hello #{subject}"

hello # => ArgumentError: `subject` is missing
hello("Simone") # => "Hello Simone"

Got any Ruby Language Question?