Ruby 3 · Errors and files

Raise a custom error

Subclass StandardError for your own exceptions, then raise and rescue it.

class ValidationError < StandardError; end

begin
  raise ValidationError, "name is required"
rescue ValidationError => e
  puts e.message
end

Official Ruby 3 documentation

More errors and files commands

All errors and files commands · Ruby 3 cheatsheet

Was this page helpful?