Custom Errors
Subclass StandardError for your own errors.
Example
class AgeError < StandardError
def initialize(msg = "bad age") = super
end
begin
raise AgeError
rescue AgeError => e
puts e.message
end
begin
raise AgeError, "negative"
rescue => e
puts e.message
endResult
bad age negative
Exercise
Raise an error.
ArgumentErrorQuiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.