# Should a Class return an exception or nil on failure to crea

**URL:** <https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253>\
**Category:** Ruby\
**Created:** [October 6, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253 "2006-10-06T14:41:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [October 6, 2006, 2:41pm UTC](https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253/1 "2006-10-06T14:41:43Z")

</div>

Suppose I’ve written a class that does something useful.

This class, let’s call it a Widget, is created like this…

a = Widget.new(some\_value)

Not all values of ‘some\_value’ are valid and I need to indicate when  
creation fails.  
I see two ways to signify this…

a = Widget.new(invalid\_value) #=\> nil  
or  
a = Widget.new(invalid\_value) #=\> raises an exception

Which of these do you prefer and why?

Would you prefer both? How about a ‘Widget.new!’ method that throws an  
exception?

Thanks,  
\_Kevin

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [October 6, 2006, 2:51pm UTC](https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253/2 "2006-10-06T14:51:46Z")

</div>

\_Kevin wrote:

> a = Widget.new(invalid\_value) #=\> nil

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.

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?

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [October 6, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253/3 "2006-10-06T15:01:48Z")

</div>

On 06.10.2006 14:50, Timothy H. 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
```

---

<div class="post-metadata">

**Author:** ![S\_Kevin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/7ea924/32.png) [@S\_Kevin](https://www.ruby-forum.com/u/S_Kevin)\
**Post date:** [October 6, 2006, 4:52pm UTC](https://www.ruby-forum.com/t/should-a-class-return-an-exception-or-nil-on-failure-to-crea/71253/4 "2006-10-06T16:52:16Z")

</div>

Yeah,

This is the approach I have generally taken. If it’s not valid, raise  
an exception. One can always do something like…

a = Widget.new(invalid) rescue nil

Thanks,  
\_Kevin
