Ruby Language Raising an exception


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"