Ruby - Create your own exception

Introduction

To create your own type of exception. For example:

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

Here you've created a BadDataException class inheriting from Ruby's standard RuntimeError exception class.

Related Topics