From: Robert Klemme Date: 2010-01-21T17:14:17+09:00 Subject: Re: Looking for object.inherits?(Classname) 2010/1/21 Xeno Campanoli : > Walton Hoops wrote: >> >> On 1/20/2010 2:36 PM, Walton Hoops wrote: >>> >>> On 1/20/2010 12:05 PM, Xeno Campanoli wrote: >>>> >>>> Robert Klemme wrote: >>>>> >>>>> 2010/1/20 Xeno Campanoli : >>>>> First of all that would not be a SyntaxError because the syntax is not >>>>> affected.  Using this error type will create misleading error >>>>> messages. >>>> >>>> If I want a method that only takes objects of family Xclass, and I get >>>> one passed that is not in that family, isn't that a syntax error?  Bad type >>>> passed in that argument place?  What's the difference between that and >>>> passing a string when it needs say an array? >>> >>> Nope, that's an ArgumentError.  Example: >>> irb(main):001:0> Integer("boo") >>> ArgumentError: invalid value for Integer: "boo" >>>        from (irb):2 >>> irb(main):002:0> >>> >>> Integer("boo") syntactically correct, yet "boo" is not a valid argument >>> for this function. >>> >> Whoops, my bad, actually the situation you describe is a TypeError: >> irb(main):002:0> Integer([1,2,3]) >> TypeError: can't convert Array into Integer >>        from (irb):3 >> irb(main):003:0> >> >> again, still syntactically sound, but Integer can't take an array. > > Okay.  Good.  Thanks.  I appreciate the feedback. This will help my > programming, perhaps semantically I guess I should say. Actually I would be less strict about the question whether a TypeError, an ArgumentError or a NoMethodError is most appropriate because I either do not catch any of them or just StandardError. Even then the important information for me is in the stack trace and not so much in the exception type. But that's probably because I create smaller apps with scripting character most of the time. It does make sense to think about this a little more (but not too much) when creating larger apps. I tend to use ArgumentError for wrong arguments because that seems the most general description of what happened. A NoMethodError will be thrown automatically when applying Duck Typing (i.e. caused by a method not understood by the receiver). I'd agree that in this case a TypeError is probably most appropriate. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/