Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ruby Methods Code Challenge

Hello,

Can anyone point me in the direction of where I can find how to raise custom errors if a method is called inappropriately? Its not mentioned in the videos anywhere, and I can't seem to figure out what to google to get a clear answer.

Thanks!

6 Answers

Tony Cronin
Tony Cronin
14,151 Points

Hi Scott,

'raise custom errors ruby' google search led me to stack overflow:

http://stackoverflow.com/questions/3382866/rubys-exception-error-classes

I won't try to paraphrase :D

Tony

Hey Tony. Thanks for your help! I saw that one, but I'm confused as to how I can get that to "plug in" along with my code? I am calling a method "protected_reset" on a variable and am not sure how to set that MyError class to do anything if the reset isn't possible to run.

Tony Cronin
Tony Cronin
14,151 Points

Scott,

can you send me your code?

tony_treehouse at iCloud dot com

Tony

Tony Cronin
Tony Cronin
14,151 Points

Scott,

I've emailed a solution to you but as this may help others I'll paste the solution here. Please note, this is not the canonical solution. Please improve if you can.

# Now that we know the differences between the different kinds of methods,
# try writing a program with a protected method. Have that method raise a
# specific custom error if it's called inappropriately.

class BankAccount
  module Error
    class MyError < StandardError; end
  end

  def self.create_for(first_name, last_name)
    @accounts ||= []
    @accounts << BankAccount.new(first_name, last_name)
  end

  def self.find_account(first_name, last_name)
    @accounts.find {|account| account.full_name == "#{first_name} #{last_name}"}
  end

 def self.account_list
    @accounts
 end

  def initialize(first_name, last_name)
      @first_name = first_name
      @last_name = last_name
      @balance = 0
  end

  def full_name
    "#{@first_name} #{@last_name}"
  end

  def deposit(amount)
    #a begin rescue block raises the error, and prints a message        
    begin
        raise Error::MyError if amount <= 0
    rescue
        p "a deposit cannot be smaller than 0. You tried to deposit #{amount}"
    end
    #now deposit to the account
    @balance += amount if amount > 0
  end

  def withdraw(amount)
     @balance -= amount
  end

  def balance
    @balance
  end

  protected
  def protected_reset
    @balance = 0
  end
end

Tony

Thanks Tony! That worked. We hadn't gotten into modules yet, so I think thats why I was confused.

Tony, what if we wanted to have a custom error message when someone tried to do 'reset!' Im not sure how to do that...