From: Ron Jeffries Date: 2001-08-15T21:11:16+09:00 Subject: [ruby-talk:19769] Re: "new" returning nil: how to report the failure of object creation On 14 Aug 2001 20:47:24 -0700, furufuru@ccsr.u-tokyo.ac.jp (Ryo Furue) wrote: >Hi there, > >How do you report the failure of object creation, that is, if you find >it impossible to properly initialize the object in the "initialize" >method? I suspect it's generally a bad idea to write code where new can fail, as using it is problematical. However, if you must, consider returning an Error Object: an object of a different class that will respond true to isError? (while successfully-built objects of course return false). The error object can optionally contain information about how things went wrong. Error Objects typically respond to no messages other than isError? and some error-related methods that normal objects don't have, and maybe a few print-related objects. They usually do not respond to the messages that the real object would have. That way, if I don't check for Error Objects, the next message I send to it will fail. A related technique is to return a Default Object that works but doesn't do anything. The core idea is called Null Object, and you can read lots about it at http://c2.com/cgi/wiki?NullObject . Ronald E Jeffries http://www.XProgramming.com http://www.objectmentor.com