When is it right for a constructor to throw an exception?

When is it right for a constructor to throw an exception? (Or in the case of Objective C: when is it right for an init’er to return nil?)

It seems to me that a constructor should fail — and thus refuse to create an object — if the object isn’t complete. I.e., the constructor should have a contract with its caller to provide a functional and working object on which methods can be called meaningfully? Is that reasonable?

23 Answers
23

Leave a Comment