From: Robert Klemme Date: 2006-10-06T22:00:09+09:00 Subject: Re: Should a Class return an exception or nil on failure to create? On 06.10.2006 14:50, Timothy Hunter wrote: > Exception. It's not really a matter of preference. YourClass.new > allocates an instance of your class and then calls initialize on the new > instance. YourClass.new will return the new instance unless initialize > raises an exception. Fully agree. More arguments in favour of exceptions: what do you do with your nil instance? You either explicitly check it for nil and - throw an exception or choose a different path of execution. If you throw an exception you could have done so in initialize right away. If you chose another course of execution, well that can be done more cleanly with begin / rescue / end. If you do not check it you get an exception anyway when you invoke the first Widget method on nil. This shows another disadvantage: if you forget to check for nil or store the instance away and do not directly use it you will be bitten by en exception much later and the original error is less easy to debug. > Now some advanced Rubyists out there will jump in and explain how to > subvert the allocate/initialize process but you're asking about the > "usual" case, right? Hehe... I won't disclose the secret. >-} robert