# How to fail initialize

**URL:** <https://rubytalk.org/t/how-to-fail-initialize/35429>\
**Category:** ruby-talk\
**Created:** [16 February 2007 15:15 UTC](https://rubytalk.org/t/how-to-fail-initialize/35429 "2007-02-16T15:15:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jim1](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Jim1](https://rubytalk.org/u/Jim1)\
**Post date:** [16 February 2007 15:15 UTC](https://rubytalk.org/t/how-to-fail-initialize/35429/1 "2007-02-16T15:15:37Z")

</div>

I would like to know the correct way to fail the creating of an object.  
I would return a NULL in c/c++ but returning a nil does not to seem to  
work correctly i.e.

if(obj) then do stuff

OR

if(obj != nil) then do stuff

New to Ruby if you couldn't guess.

Jim.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [16 February 2007 15:20 UTC](https://rubytalk.org/t/how-to-fail-initialize/35429/2 "2007-02-16T15:20:55Z")

</div>

def initialize  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;raise  
&nbsp;&nbsp;&nbsp;end

the code is easier to work with if it either returns valid results, or raises  
an exception - i removes the burden of testing return values in the client  
code.

kind regards.

-a

> **···**
>
> On Sat, 17 Feb 2007, Jim wrote:
> 
> > I would like to know the correct way to fail the creating of an object.  
> > I would return a NULL in c/c++ but returning a nil does not to seem to  
> > work correctly i.e.
> > 
> > if(obj) then do stuff
> > 
> > OR
> > 
> > if(obj != nil) then do stuff
> > 
> > New to Ruby if you couldn't guess.
> 
> --  
> we can deny everything, except that we have the possibility of being better.  
> simply reflect on that.  
> - the dalai lama

---

<div class="post-metadata">

**Author:** ![Jim1](https://avatars.discourse-cdn.com/v4/letter/j/779978/32.png) [@Jim1](https://rubytalk.org/u/Jim1)\
**Post date:** [16 February 2007 15:36 UTC](https://rubytalk.org/t/how-to-fail-initialize/35429/3 "2007-02-16T15:36:57Z")

</div>

Thanks for quick response, I decided to do error testing before creating  
object thus removing the problem, I find exception handling messy.

Thanks,  
Jim.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
