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!
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

Scott McIntyre
2,623 PointsRuby 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
14,151 PointsHi 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

Scott McIntyre
2,623 PointsHey 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
14,151 PointsScott,
can you send me your code?
tony_treehouse at iCloud dot com
Tony

Tony Cronin
14,151 PointsScott,
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

Scott McIntyre
2,623 PointsThanks Tony! That worked. We hadn't gotten into modules yet, so I think thats why I was confused.

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