Skip to content
Justice Tech School

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
end

Result

bad age
negative

Exercise

Raise an error.

 ArgumentError

Quiz Test

What do custom errors inherit?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.