Raise exception from constructor : raise « Statement « Ruby






Raise exception from constructor


class Person
  def initialize(name)
    raise ArgumentError, "No name present" if name.empty?
  end
end

fred = Person.new('')

 








Related examples in the same category

1.Raising Exceptions
2.raise one on your own with the raise method from Kernel
3.raise ArgumentError
4.Raise IndexError