From: Aristarkh A Zagorodnikov Date: 2001-08-15T21:22:00+09:00 Subject: [ruby-talk:19773] Re: "new" returning nil: how to report the failure of object creation > -----Original Message----- > From: Jim Menard [mailto:jimm@io.com] > Sent: Wednesday, August 15, 2001 4:11 PM > To: ruby-talk ML; undisclosed-recipients: > Subject: [ruby-talk:19770] Re: "new" returning nil: how to report > the failure of object creation > > > furufuru@ccsr.u-tokyo.ac.jp (Ryo Furue) writes: > > > 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? Raising an exception is clearly an option. But, what if the > > "failure" is a normal part of the flow of the program? > > At least one framework I've used (BeOS) set a status variable. It was the > programmers responsibility to check it after calling new. For example, /skipped/ Another way is to rely on exceptions as a naturally superior (in most but not all cases) solution to error handling. I never rely on status codes unless underlying API forces me to do so (if I have time I write exception-based API layer). This might sound like a bit of overkill, especially when dealing with a lot of exception types, calls and such, however in ruby exception handling is so easy and straitforward, that adding a few more exception handlers is a just a matter of a few minutes, but it greatly increases readability, clarity and overall quality of code, also effectively protecting you from stupid unhandled error situations. Aristarkh A Zagorodnikov, Lead Programmer, W3D Group http://www.w3d.ru /// xm@w3d.ru /// ICQ UIN 36987938