
January 23rd, 2012, 10:21 PM
|
|
Registered User
|
|
Join Date: Jan 2012
Posts: 2
Time spent in forums: 26 m 22 sec
Reputation Power: 0
|
|
|
Exceptions
I'm building an error handler by a book and I think it's not working as it should. According to the book, the code should output only 2 lines (wording is a bit different, I know):
Quote:
You gave me some bad input: Who knows?
! GenderError ("Invalid input!") |
but I get only the first line, followed by, I guess actual errors in my error handler. Here's the console text:
Quote:
incorrect input value: fuzzy
C:/Users/PC/Documents/Eclipse/test_project/custom exceptions.rb:14:in `define_gender': What's this? Invalid input? (GenderError)
from C:/Users/PC/Documents/Eclipse/test_project/custom exceptions.rb:19:in `initialize'
from C:/Users/PC/Documents/Eclipse/test_project/custom exceptions.rb:26:in `new'
from C:/Users/PC/Documents/Eclipse/test_project/custom exceptions.rb:26:in `<main>' |
Here's the code itself:
Code:
class GenderError < RuntimeError
attr :what_they_put
def initialize(their_input)
@what_they_put = their_input
end
end
class Person
def define_gender(gender)
if (gender.upcase != 'FEMALE') && (gender.upcase != 'MALE')
raise GenderError.new(gender), "What's this? Invalid input?"
end
end
def initialize(gender)
self.define_gender(gender)
rescue GenderError => e
puts "incorrect input value: " + e.what_they_put
raise
end
end
dude = Person.new("fuzzy")
I spent an hour now looking at this code, trying to figure out the error with no results... Can anyone help?
|