A custom exception class gives a specific name to one kind of failure.

Custom Exception

custom_exception.rb
class BalanceError < StandardError
end

def withdraw(balance, amount)
  raise BalanceError, "not enough balance" if amount > balance

  balance - amount
end

amount = 
balance = 20

begin
  remaining = withdraw(balance, amount)
  puts "remaining=#{remaining}"
rescue BalanceError => error
  puts "error=#{error.message}"
end
class BalanceError < StandardError
end

def withdraw(balance, amount)
  raise BalanceError, "not enough balance" if amount > balance

  balance - amount
end

amount = 
balance = 20

begin
  remaining = withdraw(balance, amount)
  puts "remaining=#{remaining}"
rescue BalanceError => error
  puts "error=#{error.message}"
end
class BalanceError < StandardError
end

def withdraw(balance, amount)
  raise BalanceError, "not enough balance" if amount > balance

  balance - amount
end

amount = 
balance = 20

begin
  remaining = withdraw(balance, amount)
  puts "remaining=#{remaining}"
rescue BalanceError => error
  puts "error=#{error.message}"
end
custom exception A custom exception class can inherit from `StandardError` and be rescued by name.