# How best to handle errors in object instaintiation (sp ?)

**URL:** <https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554>\
**Category:** Ruby\
**Created:** [December 6, 2005, 2:23am UTC](https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554 "2005-12-06T02:23:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [December 6, 2005, 2:23am UTC](https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554/1 "2005-12-06T02:23:11Z")

</div>

Hi,  
I’m new to Ruby and still trying to figure out how best to do things  
“the Ruby way”. The particular problem I am looking at now is the  
initialize method of a class I am writing. Several things can go wrong  
in calls to new for this class ranging from the caller specifying  
incompatible parameters to database calls failing.

What is the perferred method of handling such errors? Simply raising  
exceptions and returning nil?

Cheers, Russell

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [December 6, 2005, 3:08am UTC](https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554/2 "2005-12-06T03:08:35Z")

</div>

Russell F. wrote:

> Cheers, Russell

You don’t really have any choice. The return value from ‘new’ is an  
instance of the class, never nil. (Okay, some clever Rubyists could  
think of a way to make it nil, but no user of your class should ever be  
asked to handle that case.) If your initialize method can fail, it  
should raise an exception.

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [December 6, 2005, 3:33am UTC](https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554/3 "2005-12-06T03:33:57Z")

</div>

tim-hunter wrote:

> You don’t really have any choice. If your initialize method can fail, it  
> should raise an exception.

Great, that’s what I thought but I wanted to make sure. None of the  
examples I found in books handled errors in new().

Thanks Tim.

---

<div class="post-metadata">

**Author:** ![russellf](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/7bcc69/32.png) [@russellf](https://www.ruby-forum.com/u/russellf)\
**Post date:** [December 7, 2005, 6:22am UTC](https://www.ruby-forum.com/t/how-best-to-handle-errors-in-object-instaintiation-sp/49554/4 "2005-12-07T06:22:25Z")

</div>

On 12/5/05, Timothy H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You don’t really have any choice. The return value from ‘new’ is an  
> instance of the class, never nil. (Okay, some clever Rubyists could  
> think of a way to make it nil, but no user of your class should ever be  
> asked to handle that case.) If your initialize method can fail, it  
> should raise an exception.

Hm, no, that’s not quite right 😉

class Foo  
def self.new  
super  
rescue Exception  
nil  
end  
def initialize  
raise “Bah!”  
end  
end

It’s not ideal, and it would probably be better to do something like:

class Bar  
class \<\< self  
private :new

```
  def create(*args, &block)
    ob = self.allocate
    ob.initialize(*args, &block)
    ob
  rescue Exception
    nil
  end
end
def initialize(*args, &block)
  raise "Empty!" if args.empty
  @args = args
end
attr_accessor :args

```

end

That is, it’s better to use a different method than new.

-austin
